206. Reverse Linked List - Detailed Explanation

Problem Statement

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

In other words, you need to change the next pointers of all the nodes so that the list is reversed.

Example Inputs & Outputs

  • Example 1:

    • Input: head = [1, 2, 3, 4, 5]
    • Output: [5, 4, 3, 2, 1]
    • Explanation:
      The linked list is reversed so that the new head is the node with value 5, followed by nodes with values 4, 3, 2, and 1.
  • Example 2:

    • Input: head = [1, 2]
    • Output: [2, 1]
    • Explanation:
      The two nodes swap positions.
  • Example 3:

    • Input: head = []
    • Output: []
    • Explanation:
      An empty list remains empty after reversal.

Hints

  1. Iterative Approach Using Pointers:
    Think about maintaining two pointers—one for the previous node and one for the current node. As you traverse the list, you reverse the next pointer of the current node to point to the previous node.

  2. Recursive Approach:
    The recursive solution involves reversing the rest of the list and then fixing the current node’s pointer.

Approaches

1. Iterative Approach

Idea:

  • Initialize two pointers: prev (set to None) and curr (set to the head).
  • Traverse the list and for each node:
    • Save the next node.
    • Reverse the pointer of the current node to point to the previous node.
    • Move the prev and curr pointers one step forward.
  • Return prev as the new head when curr becomes None.

Steps:

  1. Set prev = None and curr = head.
  2. While curr is not None:
    • Temporarily store curr.next.
    • Set curr.next to prev (reverse the pointer).
    • Move prev to curr and curr to the stored next node.
  3. Return prev as the head of the reversed list.

2. Recursive Approach

Idea:

  • The base case is when the list is empty or has one node.
  • Recursively reverse the sublist starting from the second node.
  • Adjust the pointers so that the next node points back to the current node, and then set the current node’s next to None.

Steps:

  1. If head is None or head.next is None, return head.
  2. Otherwise, recursively reverse the list from head.next onward.
  3. Set head.next.next to head to reverse the link.
  4. Set head.next to None to break the original link.
  5. Return the new head from the recursive call.

Code Implementations

Python Code

Python3
Python3

. . . .

Java Code

Java
Java

. . . .

Complexity Analysis

  • Time Complexity:

    • Both the iterative and recursive approaches traverse each node exactly once, so the time complexity is O(n), where n is the number of nodes in the list.
  • Space Complexity:

    • Iterative Approach: Uses constant extra space, O(1).
    • Recursive Approach: Uses O(n) space in the recursion stack.

Step-by-Step Walkthrough (Iterative Approach)

  1. Initialization:
    Set prev = None and curr = head (the first node of the list).

  2. Iteration:

    • While curr is not null:
      • Save curr.next in a temporary variable (next_temp).
      • Change curr.next to point to prev, effectively reversing the pointer.
      • Move prev to curr and curr to next_temp.
  3. Termination:
    When curr becomes null, prev points to the new head of the reversed list.

  4. Return:
    Return prev as the head of the reversed linked list.

Common Mistakes

  1. Losing the Next Node:
    Not saving curr.next before changing curr.next leads to losing access to the rest of the list.

  2. Incorrect Pointer Updates:
    Updating pointers in the wrong order may result in a broken list or an infinite loop.

  3. Forgetting to Handle Empty Lists:
    Ensure that the algorithm correctly returns None or an empty list when the input is empty.

Edge Cases & Alternative Variations

  • Edge Cases:

    • Empty List:
      If the input list is empty, simply return None.
    • Single Node:
      A list with one node should remain unchanged after reversal.
  • Alternative Variations:

    • Partial Reversal:
      Problems that require reversing only a portion of the list.
    • Reversing in Groups:
      Reversing nodes in groups (e.g., reverse every k nodes).
  • Reverse Nodes in k-Group:
    Reverse nodes of a linked list in groups of size k.
  • Swap Nodes in Pairs:
    Swap every two adjacent nodes in a linked list.
  • Linked List Cycle Detection:
    Determine if a linked list has a cycle.
TAGS
leetcode
CONTRIBUTOR
Design Gurus Team
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
862. Shortest Subarray with Sum at Least K - Detailed Explanation
Learn to solve Leetcode 862. Shortest Subarray with Sum at Least K with multiple approaches.
1086. High Five - Detailed Explanation
Learn to solve Leetcode 1086. High Five with multiple approaches.
392. Is Subsequence - Detailed Explanation
Learn to solve Leetcode 392. Is Subsequence with multiple approaches.
2964. Number of Divisible Triplet Sums - Detailed Explanation
Learn to solve Leetcode 2964. Number of Divisible Triplet Sums with multiple approaches.
424. Longest Repeating Character Replacement - Detailed Explanation
Learn to solve Leetcode 424. Longest Repeating Character Replacement with multiple approaches.
2503. Maximum Number of Points From Grid Queries - Detailed Explanation
Learn to solve Leetcode 2503. Maximum Number of Points From Grid Queries with multiple approaches.
Related Courses
Course image
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
4.6
Discounted price for Your Region

$197

Course image
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
3.9
Discounted price for Your Region

$78

Course image
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
4
Discounted price for Your Region

$78

Image
One-Stop Portal For Tech Interviews.
Copyright © 2026 Design Gurus, LLC. All rights reserved.