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.

This question defines the In-place Reversal of a Linked List pattern, the technique that redirects existing next pointers one at a time instead of building a second list. Interviewers use it as the base case for every harder list question, and it is one of the recurring coding interview patterns worth drilling early. It also sits on the Blind 75 list.

Example inputs and 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 point the next pointer of the current node back at the previous node.

  2. Recursive approach:
    The recursive solution reverses the rest of the list first, then repairs the pointer of the current node.

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.
    • Point the next pointer of the current node back at 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, which reverses the link.
    • 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 an empty list or a list with 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. Then set the next pointer of the current node 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, which reverses that link.
      • 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:
    Changing curr.next before you save it discards access to the rest of the list.

  2. Incorrect pointer updates:
    Updating the pointers in the wrong order leaves a broken list or an infinite loop.

  3. Forgetting to handle empty lists:
    Confirm that the algorithm returns None, or an empty list, when the input is empty.

Edge cases and alternative variations

  • Edge cases:

    • Empty list:
      If the input list is empty, return None.
    • Single node:
      A list with one node stays unchanged after reversal.
  • Alternative variations:

    • Partial reversal:
      Questions that ask you to reverse only a portion of the list.
    • Reversing in groups:
      Reversing nodes in groups, for example 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 whether a linked list has a cycle.

Each of those reuses the same three pointer move you just wrote. For a short map of which pattern covers which question, read the summary of the most common LeetCode patterns. The pattern itself, with the k-group and sublist variants worked out in full, is taught in Grokking the Coding Interview.

TAGS
Coding Interview Questions
Coding Interview
CONTRIBUTOR
Arslan Ahmad
Arslan Ahmad
ex-FAANG engineering manager and author or Grokking series.

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
What is useState() in React?
What Is the Cohere Interview Process Like? (Round by Round)
Cohere's loop: production-ML coding where memorized solutions get flagged, an ML/system design round on multi-tenant serving, a genuine behavioral round, and team matching.
What is a job that no longer exists?
Why do we choose Apple company?
What is the best way to study software engineering?
Can a unique key be NULL?
Related Courses
New
Grokking the AI System Design Interview course cover
Grokking the AI System Design Interview
Learn to design AI systems the way interviewers expect: classic ML products, LLM and RAG architectures, and agentic systems, all through the lens of the system design interview.
4.6
(3,192 learners)
Discounted price for Your Region

$99

Grokking the Coding Interview: Patterns for Coding Questions course cover
Grokking the Coding Interview: Patterns for Coding Questions
The 24 essential patterns behind every coding interview question. Available in Java, Python, JavaScript, C++, C#, and Go. The most comprehensive coding interview course with 543 lessons. A smarter alternative to grinding LeetCode.
4.6
Discounted price for Your Region

$197

Grokking Modern AI Fundamentals course cover
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
4.1
Discounted price for Your Region

$72

Design Gurus logo
One-Stop Portal For Tech Interviews.
About Us
Our Team
Careers
Partners
Contact Us
Become Affiliate
Copyright © 2026 Design Gurus, LLC. All rights reserved.