0% completed
Problem Statement
Given the head
of a singly
linked list and two positive integers left
and right
where left <= right
, reverse all the nodes from the left to the right position, and return the updated list.
Examples
Example 1:
- Input:
[7, 8, 9, 10, 11]
,left = 2
,right = 4
- Expected Output:
[7, 10, 9, 8, 11]
- Justification: The nodes from position 2 to 4 (8, 9, 10) are reversed to become 10, 9, 8.
Example 2:
- Input:
[1, 2, 3, 4, 5, 6]
,left = 2
,right = 5
- Expected Output:
[1, 5, 4, 3, 2, 6]
- Justification: The nodes from position 2 to 5 (2, 3, 4, 5) are reversed to become 5, 4, 3, 2.
Example 3:
- Input:
[5, 6, 7, 8, 9]
,left = 1
,right = 3
- Expected Output:
[7, 6, 5, 8, 9]
- Justification: The nodes from position 1 to 3 (5, 6, 7) are reversed to become 7, 6, 5.
Constraints:
- The number of nodes in the list is n.
- 1 <= n <= 500
- -500 <= Node.val <= 500
- 1 <= left <= right <= n
Solution
To solve this problem, we will use a two-pointer technique along with a dummy node to simplify edge cases. The approach involves moving two pointers to the positions just before and at the start of the sublist that needs to be reversed. We'll then reverse the sublist in-place by adjusting the pointers.
This approach is effective because it allows us to reverse the specific portion of the list without needing extra space for another list, keeping the space complexity low. By adjusting pointers directly, we ensure that the solution is efficient and runs in linear time relative to the length of the list.
Step-by-Step Algorithm
-
Initialize a dummy node:
- Create a dummy node with value
0
and set itsnext
pointer to the head of the list. This helps to handle edge cases smoothly. - Initialize a pointer
prev
to point to the dummy node.
- Create a dummy node with value
-
Move
prev
to the node just before the start of the sublist to be reversed:- Iterate
left - 1
times to moveprev
to the node right before the positionleft
.
- Iterate
-
Initialize pointers for reversing the sublist:
- Set a pointer
start
toprev.next
, which points to the first node in the sublist to be reversed. - Set a pointer
then
tostart.next
, which points to the node afterstart
.
- Set a pointer
-
Reverse the sublist from position
left
toright
:- Perform the reversal
right - left
times:- Set
start.next
tothen.next
. - Set
then.next
toprev.next
. - Set
prev.next
tothen
. - Move
then
tostart.next
.
- Set
- Perform the reversal
-
Return the new head of the list:
- Return
dummy.next
, which points to the new head of the list after reversal.
- Return
Algorithm Walkthrough
Let's walk through the example input [1, 2, 3, 4, 5, 6]
, left = 2
, right = 5
step by step.
-
Initialize a dummy node:
dummy
->0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6
prev
points todummy
.
-
Move
prev
to the node just before the start of the sublist:- Iterate
left - 1 = 2 - 1 = 1
times. - After 1 iteration,
prev
points to the node with value1
. - Now,
prev
->1 -> 2 -> 3 -> 4 -> 5 -> 6
- Iterate
-
Initialize pointers for reversing the sublist:
start
->2 -> 3 -> 4 -> 5 -> 6
then
->3 -> 4 -> 5 -> 6
-
Reverse the sublist from position
left
toright
:-
Perform the reversal
right - left = 5 - 2 = 3
times. -
First iteration:
start.next
->4 -> 5 -> 6
then.next
->2 -> 4 -> 5 -> 6
prev.next
->3 -> 2 -> 4 -> 5 -> 6
- Move
then
tostart.next
->4 -> 5 -> 6
- List now looks like:
0 -> 1 -> 3 -> 2 -> 4 -> 5 -> 6
-
Second iteration:
start.next
->5 -> 6
then.next
->3 -> 2 -> 5 -> 6
prev.next
->4 -> 3 -> 2 -> 5 -> 6
- Move
then
tostart.next
->5 -> 6
- List now looks like:
0 -> 1 -> 4 -> 3 -> 2 -> 5 -> 6
-
Third iteration:
start.next
->6
then.next
->4 -> 3 -> 6
prev.next
->5 -> 4 -> 3 -> 2 -> 6
- Move
then
tostart.next
->6
- List now looks like:
0 -> 1 -> 5 -> 4 -> 3 -> 2 -> 6
-
-
Return the new head of the list:
- Return
dummy.next
, which is the node with value1
.
- Return
The final modified list after reversal is [1, 5, 4, 3, 2, 6]
.
Code
Complexity Analysis
Time Complexity: O(n)
We traverse the linked list a few times: once to reach the position before the sublist to be reversed, and then to reverse the sublist. This results in O(n) complexity, where n is the number of nodes in the list.
Space Complexity: O(1)
The algorithm uses a constant amount of extra space regardless of the input size. We only use a few pointers for reversing the sublist.
.....
.....
.....
Table of Contents
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible