Probably answered by hundreds of programmers out there, I still try to answer this question. The difference.. I illustrate with a small helper diagram(s).

The take away is that, no one should waste time on the night of interview trying to understand the code…

The code is very simple though 😛

Node * reverse( Node * ptr )
Node * temp;
Node * previous = NULL;
while(ptr != NULL) {
temp = ptr->next;
ptr->next = previous;
previous = ptr;
ptr = temp;
return previous;

Step 1:

We have a linked list as such, and now we create two NULL pointers Temp1 and Return. xPtr initially points to the element 5, or should it. Otherwise, we will reverse the list from the node pointed by xPtr


Step 2:
Now, we do the same iterative process that we are about to perform over the entire list. It involves three simple steps. Firstly, you copy the location of the next pointer of the list index xPtr, and store it somewhere, say Temp1. We need it to reverse the list. So we can now invalidate the next pointer to null, so assign the Null return pointer to xPtr->next. So far, we have removed the first node of the list and made it point to NULL. This will be the last node of the final (reversed) list. The final operations involve returning the new list in the name of Return pointer. So, assign xPtr to Return, and there you go, you have the new reverse list pointed by Return. We need to do these operations continually to get the full reversed list. Wait! we forgot something. If xPtr points to NULL, what will happen in the next iteration?! That is the reason for the temporary pointer Temp1. So, by just assigning the Temp1 pointer to xPtr, we can now relink the linked list, without, of course, the first node! Simple, right?


Step 3:
Now, we do the same set of operations at the second node of the list. Here, instead of making xPtr->next point to NULL, we point it to the already available Return pointer, which has the reverse list. So, in essence, xPtr now holds the updated reverse list, including the last node (obtained from Return pointer, which points to NULL)…


Step 4: And then…


Step 5: And then…


Step n:
After doing the same operations for a finite number of steps, we arrive at the reversed list at the xPtr.

The cost of doing this operation is O(n), with the need to declare two pointers to the linked list. There is no dynamic memory allocation required, and involves less overhead of memory.