Reverse Linked List

Pattern: Reversal

easy

Walk the list once with prev, cur and next. Save cur.next, point cur back at prev, then step both forward. When cur runs off the end, prev is the new head.

The problem

Given the head of a singly linked list, reverse it and return the new head. For example, 1 → 2 → 3 → 4 → 5 becomes 5 → 4 → 3 → 2 → 1, and an empty list stays empty.

The function name reverseList matches LeetCode. Use the link above for the exact statement and limits. The GFG link covers the same problem.

Think first

  • After the reversal, where should each node's next point?
  • When you change cur.next, what do you lose?
  • Which node is the new head?

Hints

    Brute force, and why it fails

    Push all nodes on a stack and pop them to relink: O(n) time and O(n) memory. Reversing the arrows in place needs O(1) memory. A recursive version is also O(n) but uses O(n) stack.

    The key insight

    Reversal never needs to look back: at every step the part behind cur is already reversed, and the part ahead is untouched. Three pointers keep both parts reachable.

    Dry run

    10cur
    21 
    32 
    prevnull

    The list is 1 → 2 → 3. prev is null and cur is on 1.

    Step 1 of 4

    The plan

    prev = null, cur = head
    while cur is not null:
        next = cur.next
        cur.next = prev
        prev = cur
        cur = next
    return prev
    

    Complexity

    Time
    O(n)
    Space
    O(1)

    One pass with three pointers.

    Edge cases

    • Empty list: null.
    • One node: itself.
    • Two nodes: they swap.
    I've built my logic, show the code

    Java

    class Solution {
        public ListNode reverseList(ListNode head) {
            ListNode prev = null, cur = head;
            while (cur != null) {
                ListNode next = cur.next;           // save the rest
                cur.next = prev;                    // turn this arrow around
                prev = cur;                         // step forward
                cur = next;
            }
            return prev;                            // the old tail is the new head
        }
    }

    C++

    class Solution {
    public:
        ListNode* reverseList(ListNode* head) {
            ListNode *prev = NULL, *cur = head;
            while (cur != NULL) {
                ListNode* next = cur->next;         // save the rest
                cur->next = prev;                   // turn this arrow around
                prev = cur;                         // step forward
                cur = next;
            }
            return prev;                            // the old tail is the new head
        }
    };

    Python

    from lib import ListNode
    
    class Solution:
        def reverseList(self, head: ListNode) -> ListNode:
            prev, cur = None, head
            while cur is not None:
                nxt = cur.next                      # save the rest
                cur.next = prev                     # turn this arrow around
                prev, cur = cur, nxt                # step forward
            return prev                             # the old tail is the new head

    Common mistakes

    • Not saving next first.
    • Returning head, which is now the tail.
    • Leaving the old head pointing at its old next, which creates a two-node loop.

    Similar problems