Reverse a linked list from position m to n. Do it in-place and in one-pass.
For example:
Given 1->2->3->4->5->NULL
, m = 2 and n = 4,
return 1->4->3->2->5->NULL
.
Note:
Given m, n satisfy the following condition:
1 ≤ m ≤ n ≤ length of list.
Analysis:
One of the key tricks is to set up 2 pointers, distance between which equal to n-m. Therefore, when the left pointer reached m position, the right pointer would reach n position as wee.
After we got the node at position m and the node at position n, we move node at position m to the position after node n one by one, until all the nodes before node n processed. For example:
Original linked list: 1->2->3->4->5->6->7; m = 3, n =6
Step1: 1->2->4->5->6->3->7
Step2: 1->2->5->6->4->3->7
......
Result: 1->2->6->5->4->3->7
Note that pointer m is switching to right one by one in each step, but pointer n remains no change.
Finally, we need to consider about the case that reverse range started from the very beginning of the linked list. In this case, we should return the pointer n rather than return the original head of the linked list.
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode reverseBetween(ListNode head, int m, int n) {
// init m node and n node
ListNode mNode = head;
ListNode nNode = head;
// previous node of node m
ListNode mPreNode = new ListNode(0);
mPreNode.next = head;
// set up the distance between node m and node n
for (int i=0; i<n-m; i++) nNode = nNode.next;
// locate node m and node n
for (int i=0; i<m-1; i++){
mPreNode = mNode;
mNode = mNode.next;
nNode = nNode.next;
}
// check if case of reversing from head
boolean fromHead = false;
if(mNode == head) fromHead = true;
// reverse node range
while(mNode!=nNode){
ListNode temp = nNode.next;
nNode.next = mNode;
mPreNode.next = mNode.next;
mNode.next = temp;
mNode = mPreNode.next;
}
// case of reverse started from head
if(fromHead == true) return nNode;
// other cases
return head;
}
}