Reverse Linked List II (Reverse a Sub-list)
Problem
Reverse a linked list from position m to n. Do it in one-pass.
Note: 1 ≤ m ≤ n ≤ length of list.
For example:
Thought Process
The problem follows the In-place Reversal of a LinkedList pattern. We can use a similar approach as in Reverse a Linked List I. Here are the steps we need to follow:
Skip the first
p-1
nodes, to reach the node at positionp
.Remember the node at position
p-1
to be used later to connect with the reversed sub-list.Next, reverse the nodes from
p
toq
using the same approach discussed in Reverse a Linked List I.Connect the
p-1
andq+1
nodes to the reversed sub-list.
Solution
Last updated