Back to All Questions
Reverse a Linked List
Netflix
Linked List
Two Pointers

Given the head of a singly linked list, reverse the list, and return the reversed list's head.

Example:

Input: head = [1,2,3,4,5]
Output: [5,4,3,2,1]
Solution Walkthrough

The iterative approach reverses the list in place by walking through it once and flipping each node's next pointer to point backward instead of forward.

  1. Keep three pointers: prev (initially null), curr (initially head), and a temporary next.
  2. While curr is not null:
    • Save curr.next into next before you overwrite it.
    • Point curr.next back to prev.
    • Move prev up to curr, and curr up to next.
  3. When the loop ends, curr is null and prev is sitting on the new head — return it.

This runs in O(n) time with O(1) extra space, since every node is visited exactly once and no new list is allocated.

1
2
3
4
5
6
7
8
9
10
11
12
13
Related Algorithms