LeetCode #206 Easy

Reverse Linked List

Reverse Linked List is LeetCode 206 (Easy). You get the head of a list and must reverse a singly linked list in place, then return the new head.

  • The list can be empty, and holds at most 5000 nodes.
  • The follow-up asks for two versions: one with a loop and one with recursion.
Constraints
  • The number of nodes in the list is in the range [0, 5000].
  • -5000 <= Node.val <= 5000
linked-listrecursion
Open on LeetCode ↗
02

Intuition

Nothing has to move. Each node already sits in memory; only its next pointer decides the order. Reversing the list means turning every next pointer around so it points at the node before it.

Walking forward, the node before is always known, because it is the one you just left. That makes a single pass enough. The only danger is order of work: the moment a node's next is overwritten, the unvisited part of the list is cut off. So each round remembers the next node first, then turns the pointer, then steps forward.

How to spot this pattern

Any task that walks a singly linked list backwards, or needs its second half in reverse (palindrome check, reorder, k-group reversal), starts from this three-pointer loop.

03

Approach

Try it first

Before reading on, write the loop with three variables: the node behind, the current node, and the node ahead. Decide which line must come first in the loop body, and what the loop returns when the list is empty.

1

Start with nothing behind the head

Set prev = None and curr = head. The old head will end up as the last node, and the last node must point to null, so the first pointer written should be null, which is what prev holds.

2

Remember the node ahead

Store nxt = curr.next before changing anything. It is the only reference to the part of the list that has not been reversed yet.

3

Point the current node backwards

Set curr.next = prev. The current node is now the front of the reversed part, which grows by one node each round.

4

Step both pointers forward

Set prev = curr and curr = nxt, and repeat while curr is not null. When curr falls off the end, every pointer has been turned and prev stands on the old tail, so return prev.

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
▶3 prev, curr = None, head
▶4 while curr:
▶5 nxt = curr.next
▶6 curr.next = prev
▶7 prev, curr = curr, nxt
▶8 return prev
null1234nullprevcurrprev starts empty, curr on the head
prevnull
curr1
The head will become the last node, and the last node must point to null. Starting prev on null means the first pointer we write is exactly that.
null1234nullprevcurrnextremember 2 before touching 1's pointer
curr1
next2
1 is the only node that still knows where 2 is. Once its pointer is turned, the saved copy is the only way forward.
null1234nullprevcurrnext1 now points to nothing: it is the new end
1 points tonull
The old head becomes the tail, so it points to null. The arrow to 2 is gone, but next still holds it.
null1234nullprevcurrprev moves to 1, curr to 2
prev1
curr2
Both step one node to the right. Everything left of curr is already reversed; everything from curr on is untouched.
null1234nullprevcurrnextremember 3 before touching 2's pointer
curr2
next3
2 is the only node that still knows where 3 is. Once its pointer is turned, the saved copy is the only way forward.
null1234nullprevcurrnext2 now points back to 1
2 points to1
2 joins the front of the reversed part (1). Its old arrow to 3 is gone; next still holds it.
null1234nullprevcurrprev moves to 2, curr to 3
prev2
curr3
Both step one node to the right. Everything left of curr is already reversed; everything from curr on is untouched.
null1234nullprevcurrnextremember 4 before touching 3's pointer
curr3
next4
3 is the only node that still knows where 4 is. Once its pointer is turned, the saved copy is the only way forward.
null1234nullprevcurrnext3 now points back to 2
3 points to2
3 joins the front of the reversed part (2 → 1). Its old arrow to 4 is gone; next still holds it.
null1234nullprevcurrprev moves to 3, curr to 4
prev3
curr4
Both step one node to the right. Everything left of curr is already reversed; everything from curr on is untouched.
null1234nullprevcurrnext4 is last: nothing ahead to remember
curr4
nextnull
4 is the last node, so the saved node is null: after this round the walk ends.
null1234nullprevcurrnext4 now points back to 3
4 points to3
4 joins the front of the reversed part (3 → 2 → 1). Its old arrow to null is gone; next still holds it.
null1234nullprevcurrcurr fell off the end: every pointer is turned
prev4
currnull
curr is null, so the loop stops. prev stands on 4, the old tail.
null1234nullnew headreturn 4 → 3 → 2 → 1
return4
prev is the new head. Returning the old head would give a one-node list, because the old head now points to null.
05

Recursion

Trust the call on head.next to reverse everything after head and return the new head (the old tail). At that point head.next still points at the node that is now the end of the reversed part, so head.next.next = head hangs head after it, and head.next = None makes head the new end. A list of 0 or 1 nodes is already reversed. Each node adds one stack frame, and Python allows only 1000 by default, so a 5000-node list needs sys.setrecursionlimit outside a judge that raises it.

▶1class Solution:
▶2 def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
▶3 if not head or not head.next:
▶4 return head
▶5 new_head = self.reverseList(head.next)
▶6 head.next.next = head
▶7 head.next = None
▶8 return new_head
06

Common pitfalls

Turning the pointer before saving the next node

✗ Wrong
curr.next = prev
prev, curr = curr, curr.next
✓ Right
nxt = curr.next
curr.next = prev
prev, curr = curr, nxt

After the first line, curr.next is prev, so curr steps backwards instead of forwards. On [1, 2, 3] the first round moves curr to null and the answer is [1]; nodes 2 and 3 are lost.

Returning head instead of prev

✗ Wrong
return head
✓ Right
return prev

head still names the old first node, which is now the last one and points to null. On [1, 2, 3] this returns [1].

Recursive version: leaving the old link in place

✗ Wrong
head.next.next = head
return new_head
✓ Right
head.next.next = head
head.next = None
return new_head

Without head.next = None, the original head still points at the second node while the second node points back at it. On [1, 2] the result is 2 → 1 → 2 → 1 …, a cycle that never ends.

07

Edge cases

Empty list

curr starts as null, the loop never runs, and prev (null) is returned.

One node

One round: its next becomes null (it already was), and that node is returned as the head.

08

Complexity

Time
O(n)
Space
O(1)
Each node is visited once and gets exactly one pointer change. The loop keeps three pointers no matter how long the list is; the recursive version instead holds one stack frame per node, O(n).
09

Reverse Linked List FAQ

Why not copy the values into an array and write them back in reverse?

It gives the right output but needs O(n) extra memory, and it changes the values stored in the nodes rather than the links between them. If the nodes carry other data, or other code holds references to them, that is not the same as reversing the list.

How do you reverse a doubly linked list?

Walk the list and swap each node's prev and next fields. The last node you swap becomes the new head. No separate saved pointer is needed, because the old next is still available as the new prev.

How do you reverse only part of a list, or one block at a time?

Run the same loop on the section, then reconnect its ends to the nodes on either side. Doing that for every block of k nodes is Reverse Nodes in k-Group.