forked from DengWangBao/Leetcode-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReorderList.java
32 lines (28 loc) · 863 Bytes
/
ReorderList.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
public class ReorderList {
public void reorderList(ListNode head) {
if (head == null || head.next == null) return;
ListNode p1 = head;
ListNode p2 = head;
while (p2.next != null && p2.next.next != null) {
p1 = p1.next;
p2 = p2.next.next;
}
ListNode preMiddle = p1;
ListNode preCurrent = p1.next;
while (preCurrent.next != null) {
ListNode current = preCurrent.next;
preCurrent.next = current.next;
current.next = preMiddle.next;
preMiddle.next = current;
}
p1 = head;
p2 = preMiddle.next;
while (p1 != preMiddle) {
preMiddle.next = p2.next;
p2.next = p1.next;
p1.next = p2;
p1 = p2.next;
p2 = preMiddle.next;
}
}
}