Given the head of a singly linked list and two integers left and right where left <= right, reverse the nodes of the list from position left to position right, and return the reversed list.
Example 1:
Input: head = [1,2,3,4,5], left = 2, right = 4 Output: [1,4,3,2,5]
Example 2:
Input: head = [5], left = 1, right = 1 Output: [5]
Constraints:
n.1 <= n <= 500-500 <= Node.val <= 5001 <= left <= right <= nFollow up: Could you do it in one pass?
Problem Overview: Reverse a section of a singly linked list between positions left and right (1-indexed) while keeping the rest of the list unchanged. The challenge is updating pointers correctly so the reversed sublist reconnects with the original list.
Approach 1: Iterative In-Place Reversal (O(n) time, O(1) space)
This approach walks the list once and reverses only the portion between left and right. First, iterate to the node just before the reversal region using a pointer like prev. From there, perform an in-place reversal using the classic linked list reversal technique: repeatedly move the next node in the segment to the front of the sublist. The key insight is that you do not reverse the entire list; you only manipulate pointers within the specified window while keeping track of the connection points before and after the segment.
A dummy node simplifies edge cases where the reversal starts at the head. During each iteration, you detach the next node and insert it at the beginning of the reversed portion. This effectively builds the reversed sublist while preserving constant extra memory. Since every node is visited at most once, the time complexity is O(n) and the space complexity remains O(1). This method is the most common implementation expected in interviews involving linked list pointer manipulation.
Approach 2: Recursive In-Place Reversal (O(n) time, O(n) space)
The recursive strategy breaks the problem into two smaller tasks: moving the head of the list until it reaches the left boundary, then reversing the next right - left + 1 nodes recursively. A helper function performs the reversal of the first k nodes and returns the new head of that reversed segment. During recursion unwinding, pointers reconnect the reversed segment with the remaining list.
This technique relies on the call stack to keep track of previous nodes, which introduces O(n) auxiliary space due to recursion depth. The advantage is conceptual clarity—once you define a function that reverses the first k nodes, the rest of the logic becomes straightforward. Problems that involve partial reversal often combine recursion with pointer tracking, making this a useful pattern when practicing recursion on linked structures.
Recommended for interviews: The iterative in-place reversal is what most interviewers expect. It runs in O(n) time with O(1) space and demonstrates strong pointer control in linked lists. The recursive solution shows conceptual understanding but uses additional stack space. Explaining both approaches—starting with the straightforward recursive idea and then optimizing to the iterative constant-space version—demonstrates strong problem-solving depth.
This approach involves reversing the sublist in place by iterating from the left to the right node. We will use a dummy node to ensure that edge cases where the reversal starts at the head of the list are handled smoothly. During the iteration, we carefully manipulate the pointers to reverse the section of the list between the given positions.
The code initializes a ListNode class to represent each node. The reverseBetween method begins by creating a dummy node that precedes the head of the list, which allows us to manage edge cases where the head itself needs modification.
We identify the node before the left position, referred to as prev, and initiate a standard pointer reversal technique for the sublist. This involves switching the next pointers within the range of left and right to achieve the desired reversal.
Time Complexity: O(n), where n is the number of nodes in the list. We do a single pass over the list between left and right.
Space Complexity: O(1), since no additional data structures are used.
In this approach, we leverage recursion to reverse the sublist between the given positions. The primary idea is to handle the reversal within the recursive call stack, ensuring the in-place modification of the linked list while managing the sublist reversal inherently through recursive stack control.
Here, we define a recursive inner function reverse which handles the reversal of the initial segment of the list up to the right-left position. The recursion unfolds with the base case for n = 1. The main function drive adjusts to skip the initial non-reversal nodes as needed before invoking the recursive calls.
Python
Time Complexity: O(n), where n is the total number of nodes in the list.
Space Complexity: O(n), due to recursive call stack depth based on n.
Define a dummy head node dummy, pointing to the head node head of the linked list. Then define a pointer pre pointing to dummy. Start traversing the linked list from the dummy head node. When you traverse to the left node, point pre to this node. Then start traversing right - left + 1 times from this node, and insert the nodes you traverse into the back of pre. Finally, return dummy.next.
The time complexity is O(n), and the space complexity is O(1). Here, n is the length of the linked list.
Python
Java
C++
Go
TypeScript
Rust
JavaScript
C#
| Approach | Complexity |
|---|---|
| Iterative In-Place Reversal | Time Complexity: O(n), where n is the number of nodes in the list. We do a single pass over the list between left and right. |
| Recursive In-Place Reversal | Time Complexity: O(n), where n is the total number of nodes in the list. |
| Simulation | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Iterative In-Place Reversal | O(n) | O(1) | Best general solution; optimal for interviews and large lists with strict memory constraints |
| Recursive In-Place Reversal | O(n) | O(n) | Useful for understanding recursive linked list reversal patterns or practicing recursion techniques |
Reverse Linked List II - Leetcode 92 - Python • NeetCode • 126,274 views views
Watch 9 more video solutions →Practice Reverse Linked List II with our built-in code editor and test cases.
Practice on FleetCode