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
class Solution {
public void reorderList(ListNode head) {
if (head == null || head.next == null) {
return;
}
ListNode slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
ListNode left = head, right = reverseList(slow), dummy = new ListNode(0), ptr = dummy;
while (left != right && right != null) {
ptr.next = left;
left = left.next;
ptr = ptr.next;
ptr.next = right;
right = right.next;
ptr = ptr.next;
}
if (left == right) {
ptr.next = left;
}
}

private ListNode reverseList(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode prev = null, curr = head;
while (curr != null) {
ListNode next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
return prev;
}
}

快慢指针, 再reverse list再merge.

时间复杂度: O(n)
空间复杂度: O(1)