Solution
Approach #1 (Iterative) [Accepted]
Assume that we have linked list 1 → 2 → 3 → Ø
, we would like to change it to Ø ← 1 ← 2 ← 3
.
While you are traversing the list, change the current node's next pointer to point to its previous element. Since a node does not have reference to its previous node, you must store its previous element beforehand. You also need another pointer to store the next node before changing the reference. Do not forget to return the new head reference at the end!
public ListNode reverseList(ListNode head) { ListNode prev = null; ListNode curr = head; while (curr != null) { ListNode nextTemp = curr.next; curr.next = prev; prev = curr; curr = nextTemp; } return prev; }
Complexity analysis

Time complexity : . Assume that is the list's length, the time complexity is .

Space complexity : .
Approach #2 (Recursive) [Accepted]
The recursive version is slightly trickier and the key is to work backwards. Assume that the rest of the list had already been reversed, now how do I reverse the front part? Let's assume the list is: n_{1} → … → n_{k1} → n_{k} → n_{k+1} → … → n_{m} → Ø
Assume from node n_{k+1} to n_{m} had been reversed and you are at node n_{k}.
n_{1} → … → n_{k1} → n_{k} → n_{k+1} ← … ← n_{m}
We want n_{k+1}’s next node to point to n_{k}.
So,
n_{k}.next.next = n_{k};
Be very careful that n_{1}'s next must point to Ø. If you forget about this, your linked list has a cycle in it. This bug could be caught if you test your code with a linked list of size 2.
public ListNode reverseList(ListNode head) { if (head == null  head.next == null) return head; ListNode p = reverseList(head.next); head.next.next = head; head.next = null; return p; }
Complexity analysis

Time complexity : . Assume that is the list's length, the time complexity is .

Space complexity : . The extra space comes from implicit stack space due to recursion. The recursion could go up to levels deep.