Skip to main content

Reverse Linked List - Solution & Explanation

EasyLinked ListRecursion21 min readAsked at: Amazon, Microsoft, Apple +31
Practice this problem

Problem Statement

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

 

Example 1:

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

Example 2:

Input: head = [1,2]
Output: [2,1]

Example 3:

Input: head = []
Output: []

 

Constraints:

  • The number of nodes in the list is the range [0, 5000].
  • -5000 <= Node.val <= 5000

 

Follow up: A linked list can be reversed either iteratively or recursively. Could you implement both?

Approach Overview

Problem Overview: Given the head of a singly linked list, reverse the list so that the last node becomes the first and every next pointer points to the previous node. Return the new head after reversal. The challenge is pointer manipulation without losing access to the remaining nodes.

Approach 1: Iterative Pointer Reversal (O(n) time, O(1) space)

This approach walks through the list once and reverses the next pointer of every node. Maintain three pointers: prev, curr, and next. For each node, store curr.next in next, redirect curr.next to prev, then advance both pointers forward. The key insight is preserving the next node before modifying the pointer so you never lose the remaining list. After the loop finishes, prev points to the new head of the reversed list. This is the most common solution because it runs in linear time and uses constant extra memory. Problems involving pointer manipulation like this appear frequently in Linked List interview questions.

Approach 2: Recursive Reversal (O(n) time, O(n) space)

The recursive approach reverses the list starting from the second node and fixes the first node during the backtracking phase. Call the recursive function on head.next until the base case (last node) is reached. When the stack unwinds, set head.next.next = head to reverse the pointer direction and then set head.next = null to terminate the list. The recursive call returns the new head, which propagates back through each stack frame. This approach is elegant and highlights how recursion can naturally handle pointer re-linking, but it uses additional call stack space proportional to the list length. It’s a common demonstration of recursion in Recursion and pointer-based structures.

Recommended for interviews: The iterative approach is typically expected because it achieves O(n) time with O(1) extra space and demonstrates strong pointer manipulation skills. Interviewers often accept the recursive version as well, but they may follow up by asking you to reduce space usage. Showing both approaches signals a deeper understanding of how linked list structures work and how pointer direction can be reversed safely.

Approach 1: Iterative Approach

Iterative Approach: This approach involves using a loop to traverse the linked list and reverse the direction of the next pointers at each step. Start with three pointers: prev as null, curr as the head of the list, and nxt to temporarily store the next node. In each iteration, change the curr.next to prev, move prev to curr, and curr to nxt. The loop ends when curr becomes null, with prev being the new head.

In this C implementation, three pointers are utilized: prev, curr, and nxt. Initially, prev is NULL while curr points to the head. A loop runs until curr is NULL. Inside the loop, nxt stores the next node, then curr->next is set to prev to reverse the pointer, and prev is moved to curr, curr to nxt.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), where n is the number of nodes in the linked list because each node is processed once.
Space Complexity: O(1) as it uses only a constant amount of space.

Try this approach in the editor β†’

Approach 2: Recursive Approach

Recursive Approach: In this method, you move to the end of the list via recursive calls while reversing the next pointers on the way back. The base case for the recursion is when the list is empty or when it contains only one node. In each call, move to the next node, reverse the rest of the list, use the next pointer's next field to point to the current node, and finally return the head of the reversed list.

In the recursive C implementation, the function calls itself with the next node as an argument until it reaches the end of the list. Once there, it starts reversing the pointers. Each function returns the head of the reversed part of the list, making the whole list reversed when it unwinds.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), due to n recursive calls.
Space Complexity: O(n), for stack space in recursion.

Try this approach in the editor β†’

Approach 3: Head Insertion Method

We create a dummy node dummy, then traverse the linked list and insert each node after the dummy node. After traversal, return dummy.next.

The time complexity is O(n), where n is the length of the linked list. The space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Rust

JavaScript

C#

Try this approach in the editor β†’

Approach 4: Recursion

We recursively reverse all nodes from the second node to the end of the list, then attach the head to the end of the reversed list.

The time complexity is O(n), and the space complexity is O(n). Where n is the length of the linked list.

Code

Python

Java

C++

Go

TypeScript

Rust

C#

Try this approach in the editor β†’

Complexity Comparison

ApproachComplexity
Iterative Approach

Time Complexity: O(n), where n is the number of nodes in the linked list because each node is processed once.
Space Complexity: O(1) as it uses only a constant amount of space.

Recursive Approach

Time Complexity: O(n), due to n recursive calls.
Space Complexity: O(n), for stack space in recursion.

Head Insertion Methodβ€”
Recursionβ€”

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Iterative Pointer ReversalO(n)O(1)Preferred solution in interviews and production when constant memory is required
Recursive ReversalO(n)O(n)Useful for demonstrating recursion concepts or when recursive style is preferred

Video Solution

Reverse Linked List - Iterative AND Recursive - Leetcode 206 - Python β€’ NeetCode β€’ 700,751 views views

Watch 9 more video solutions β†’

Frequently Asked Questions

Is Reverse Linked List easy or hard?
Reverse Linked List is classified as an Easy problem on LeetCode. The core idea is straightforward once you understand pointer updates, but beginners often struggle with losing references to the remaining nodes during reversal.
Reverse Linked List Python/Java solution
Both Python and Java implementations follow the same pointer manipulation logic. Maintain three references (prev, curr, next), iterate through the list, and reverse the next pointer at each step. The algorithm remains O(n) time and O(1) space across languages.
How to solve Reverse Linked List in O(n)?
Traverse the list once while updating pointer directions. Store the next node temporarily, set the current node's next pointer to the previous node, then move both pointers forward. After processing all nodes, the previous pointer becomes the new head of the reversed list, achieving O(n) time and O(1) space.
What is the best approach for Reverse Linked List?
The iterative pointer reversal approach is the best solution for most cases. It traverses the list once and reverses each node's next pointer using three variables (prev, curr, next). The algorithm runs in O(n) time and uses O(1) extra space, which is optimal for this problem.
Is Reverse Linked List asked at Google/Amazon/Meta?
Reverse Linked List is one of the most common linked list interview questions and has appeared in interviews at companies like Amazon, Google, Meta, and Microsoft. It tests pointer manipulation, understanding of linked list structure, and ability to manage references safely.
What data structure is used in Reverse Linked List?
The problem uses a singly linked list where each node stores a value and a pointer to the next node. The algorithm works by reassigning these next pointers so that each node points to its previous node instead of the next one.
What is the time complexity of Reverse Linked List?
Reversing a linked list requires visiting each node exactly once, giving a time complexity of O(n), where n is the number of nodes. The optimal iterative solution uses O(1) additional space, while the recursive approach uses O(n) space due to the call stack.

Ready to solve this problem?

Practice Reverse Linked List with our built-in code editor and test cases.

Practice on FleetCode