Problem
Reverse a linked list from position m to n. Do it in-place and in one-pass.
OR
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.
Examples
Example 1:
--- title: Input --- graph LR A1[1] --> B2[2]:::reversed --> C3[3]:::reversed --> D4[4]:::reversed --> E5[5] classDef reversed fill:#ffcc00,stroke:#000,stroke-width:2px;
--- title: Output --- graph LR A1[1] --> D4[4]:::reversed --> C3[3]:::reversed --> B2[2]:::reversed --> E5[5] classDef reversed fill:#ffcc00,stroke:#000,stroke-width:2px;
|
|
Example 2:
|
|
Note:
Given m, n satisfy the following condition: 1 ≤ m ≤ n ≤ length of list.
Note 2:
Usually the version often seen in the interviews is reversing the whole linked list which is obviously an easier version of this question.
Solution
Method 1 - Iteration
Here is the approach:
- Initialization:
- Use a dummy node to handle edge cases where the reversal might include the head node.
- Use pointers to keep track of the node just before the reverse segment and the end of the reverse segment.
- Move to Start of Reversal Segment:
- Move a pointer to the
left-1
position.
- Move a pointer to the
- Reverse the Segment:
- Reverse the nodes between
left
andright
.
- Reverse the nodes between
- Reconnect the Links:
- Reconnect the reversed segment back into the original list.
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(n)
, wheren
is the number of nodes in the linked list. Each node is processed at most twice. - 🧺 Space complexity:
O(1)
, as we only use a constant amount of extra space for pointers.