LeetCode: Reverse Linked List II

LeetCode: Reverse Linked List II

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
54
55

/**
* Created by hzhou on 2015/5/26.
* Email: [email protected]
* <p>
* Given 1->2->3->4->5->NULL, m = 2 and n = 4,
* <p>
* return 1->4->3->2->5->NULL.
*/
public class ReverseLinkedListII {
public ListNode reverseBetween(ListNode head, int m, int n) {
if (head == null || head.next == null) {
return head;
}
int length = 0;
ListNode cursor = head;
while (cursor != null) {
length++;
cursor = cursor.next;
}
if (m >= length) {
return head;
}
ListNode preHead = new ListNode(0);
preHead.next = head;
ListNode pre = preHead;
int count = n - m + 1;
while (m > 1) {
pre = pre.next;
m--;
}
cursor = pre.next;
List<ListNode> list = new ArrayList<ListNode>();
while (count > 0 && cursor != null) {
list.add(cursor);
count--;
cursor = cursor.next;
}
for (int i = list.size() - 1; i >= 0; i--) {
pre.next = list.get(i);
pre = pre.next;
}
pre.next = cursor;
return preHead.next;
}
@Test
public void test() {
ListNode head = new ListNode(1);
head.next = new ListNode(2);
head.next.next = new ListNode(3);
head.next.next.next = new ListNode(4);
head.next.next.next.next = new ListNode(5);
ListNode result = reverseBetween(head, 1, 4);
}
}