Skip to content
  • Linked List · Sheet 150

Reverse Linked List

LearnViaNotes @learnvianotes· 2 min read

Difficulty: Easy · LeetCode 206

Problem

Given the head of a singly linked list, reverse the list and return its new head.

Understanding the problem

Invert all next pointers.

Intuition

Iterative three pointers: previous, current, next_node.

Approach

  1. Initialize previous = None, current = head.

  2. While current exists:

  3. Save next_node = current.next.

  4. Reverse pointer: current.next = previous.

  5. Advance previous = current, current = next_node.

  6. Return previous as new head.

Naive Solution — Brute Force

Recursive call stack reversal.

Python
class Solution:
    def reverseList(self, head):
        if not head or not head.next:
            return head

        new_head = self.reverseList(head.next)

        head.next.next = head
        head.next = None

        return new_head
C++
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        if (!head || !head->next) {
            return head;
        }

        ListNode* newHead = reverseList(head->next);

        head->next->next = head;
        head->next = nullptr;

        return newHead;
    }
};
Java
class Solution {
    public ListNode reverseList(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }

        ListNode newHead = reverseList(head.next);

        head.next.next = head;
        head.next = null;

        return newHead;
    }
}
JavaScript
var reverseList = function(head) {
    if (!head || !head.next) {
        return head;
    }

    const newHead = reverseList(head.next);

    head.next.next = head;
    head.next = null;

    return newHead;
};

Time: O(n) · Space: O(n) stack

Optimized Code (Python 3)

Python
class Solution:
    def reverseList(self, head):
        previous = None
        current = head

        while current:
            next_node = current.next

            current.next = previous

            previous = current
            current = next_node

        return previous
C++
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode* previous = nullptr;
        ListNode* current = head;

        while (current) {
            ListNode* nextNode = current->next;

            current->next = previous;

            previous = current;
            current = nextNode;
        }

        return previous;
    }
};
Java
class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode previous = null;
        ListNode current = head;

        while (current != null) {
            ListNode nextNode = current.next;

            current.next = previous;

            previous = current;
            current = nextNode;
        }

        return previous;
    }
}
JavaScript
var reverseList = function(head) {
    let previous = null;
    let current = head;

    while (current) {
        const nextNode = current.next;

        current.next = previous;

        previous = current;
        current = nextNode;
    }

    return previous;
};

Time: O(n) · Space: O(1)

Takeaway

Pattern — Three pointers: previous → current → next

Free for students and teachers

Write notes like this one

Cards for every idea, quizzes and flashcards, read aloud — on the web and your phone.

Something wrong with this note? Open it to report