forked from ely-mitu/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReverseLinkedListII.java
53 lines (51 loc) · 1.33 KB
/
ReverseLinkedListII.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Source : https://leetcode.com/problems/reverse-linked-list-ii/description/
// Author : Tianming Cao
// Date : 2018-02-11
/**********************************************************************************
*
* 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.
*
**********************************************************************************/
package reverseLinkedListII;
public class ReverseLinkedListII {
public ListNode reverseBetween(ListNode head, int m, int n) {
// Assume that the list is:[1,2,3,4,5,6,7] m=3 n=5
if (head == null || m == n) {
// bounds check
return head;
}
// We'd better create a default head due to m may equals 1
ListNode defaultHead = new ListNode(0);
defaultHead.next = head;
int index = 1;
ListNode pre = defaultHead;
while (index < m) {
pre = pre.next;
index++;
}
// pre is 2, first is 3
ListNode first = pre.next;
ListNode p = first;
ListNode next = p.next;
while (index < n) {
ListNode t = next.next;
next.next = p;
p = next;
next = t;
index++;
}
// next is 6
first.next = next;
pre.next = p;
return defaultHead.next;
}
}