I made a function on how to reverse a singly linked list recursively in C.
the function is as below.
struct node * reverseSLL2(struct node *p,struct node
This is undefined behavior. Compiling the code should produce a warning about reaching the end of a function through a code path that is missing a return
.
The reason you do not see that the function is broken, and observe it producing the right results, is because the actual value that is returned from the base case branch (i.e. the else
) does have a correct return
. It is possible that the compiled code reuses that last return value in all invocations of the function up the stack, so the caller ends up getting the intended value.
However, the code is invalid. You should add return
in front of the recursive call to fix the problem:
return reverseSLL2(temp1,p);