Reversing a linked list means flipping the direction of every pointer. In the original list, each node points to the next one: 1->2->3->4->5->null. After reversal, each node should point to the previous one: 5->4->3->2->1->null. The old tail becomes the new head.
The challenge is that singly linked lists only have forward pointers. You can't look backward. So to reverse a node's pointer, you need to have already saved a reference to the previous node before you move forward. If you don't, you lose track of where to point.
Reversal is a building block in many other linked list problems, including reverse nodes in k-group, palindrome linked list, and reorder list.
0 <= n <= 5000 → The list can be empty, so the algorithm must return null when head is null.Walk through the list one node at a time, and at each node, flip its next pointer to point backward instead of forward. This needs three pointers:
prev -- the node the current node should point to (starts as null, since the original head becomes the new tail and must point to null)curr -- the node being processednext -- a saved copy of curr.next, captured before the pointer is overwrittenThe moment we set curr.next = prev, the original curr.next is gone. Saving it first in next preserves the only reference to the rest of the list.
prev = null and curr = head.curr is not null:next = curr.next (so we don't lose the rest of the list).curr.next = prev.prev forward: prev = curr.curr forward: curr = next.curr is null, prev points to the last node we processed, which is the new head. Return prev.prev, curr, next) regardless of the list size.This approach is optimal in both time and space. O(n) time is required since every node must be visited, and O(1) extra space cannot be improved.
The follow-up asks for a recursive solution. The next approach expresses reversal as a recursive subproblem: reverse the rest of the list, then fix the current node's pointer.
The reversal splits into a subproblem and one fix-up step: reverse everything after the current node, then make the next node point back to the current node and set the current node's next to null.
Take the list 1->2->3->4->5. Once everything after node 1 is reversed, the list is 1->2<-3<-4<-5. Node 1 still points forward to node 2, and node 2 is now the tail of the reversed sublist (its next is null). Setting 2.next = 1 and 1.next = null reverses the final pointer and completes the list.
The base case is a list of 0 or 1 nodes, which is already reversed and returned unchanged. From there, the recursion unwinds from the tail back to the head, fixing one pointer per level. The new head, found at the deepest call, is passed back up unchanged at every level.
head is null or head.next is null, return head (a list of 0 or 1 nodes is already reversed).head.next. This returns the new head of the reversed sublist.head.next.next = head (make the next node point back to current).head.next = null (current node becomes the new tail of its portion).A stack reverses order by construction: the last item pushed is the first popped. Pushing every node, then popping them, yields the nodes in reverse order. Linking each popped node to the next popped node rebuilds the list backward.
This approach makes the reversal explicit rather than rewiring pointers in place. It uses O(n) extra space for the stack, so it is not an improvement over the iterative method, but it shows how an explicit data structure can stand in for the recursion stack, and it is a useful pattern when the same nodes must be processed in reverse later in a larger problem.
head is null, return null.next to it.next to null to terminate the list.