You are given the
head
of a linked list and two integers
m
and
n
, write a program to
reverse the linked list
from position
m
to
n
, both inclusive.
Problem Note:
-
It is guaranteed that 1
≤
m
≤n
≤ length of linked list - Do not use extra space. Do this in-place and in one iteration.
Example 1
Input: 2->4->6->8->10, m = 2, n = 4
Output: 2->8->6->4->10
Explanation: All the elements between position 2 and 4 of the given linked list are reversed.
Example 2
Input: 2->3->7->9->23, m = 1, n = 5
Output: 23->9->7->3->2
Explanation: As m and n are the first and last position of the linked list, the whole linked list is reversed.
Example 3
Input: 10->20->2->5->9->8, m = 3, n = 3
Output: 10->20->2->5->9->8
Explanation: As m = n, there is no change after reversing the linked list from m to n.