-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path143.java
More file actions
29 lines (28 loc) · 875 Bytes
/
143.java
File metadata and controls
29 lines (28 loc) · 875 Bytes
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
// three step: 1. find the middle node 2. cut and reverse the latter node link 3. merge two list node
class Solution {
public void reorderList(ListNode head) {
if (head == null || head.next == null) return;
ListNode slow = head;
ListNode fast = head.next;
while (fast != null && fast.next != null){
slow = slow.next;
fast = fast.next.next;
}
ListNode head2 = slow.next;
slow.next = null;
ListNode p = null, p1 = head2.next;
head2.next = null;
while (p1 != null){
p = p1.next;
p1.next = head2;
head2 = p1;
p1 = p;
}
for (p = head, p1 = head2; p1 != null;){
ListNode t = p.next;
p.next = p1;
p1 = p1.next;
p = p.next.next = t;
}
}
}