-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMain26.java
More file actions
32 lines (30 loc) · 845 Bytes
/
Main26.java
File metadata and controls
32 lines (30 loc) · 845 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
30
31
32
package JZOfferTuJi;
public class Main26 {
public void reorderList(ListNode head) {
ListNode slow = head, fast = head;
while (fast.next != null && fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
}
ListNode l2 = reverseList(slow.next);
slow.next = null;
ListNode l1 = head;
while (l2 != null && l1 != null) {
ListNode next = l1.next, temp = l2;
l2 = l2.next;
l1.next = temp;
temp.next = next;
l1 = next;
}
}
private ListNode reverseList(ListNode head){
ListNode cur = null;
while (head != null) {
ListNode tmp = head;
head = head.next;
tmp.next = cur;
cur = tmp;
}
return cur;
}
}