leetcode143. 重排链表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public void reorderList(ListNode head) {
LinkedList<ListNode> queue = new LinkedList<ListNode>();
ListNode tmp = head;
while (tmp != null){
queue.offer(tmp);
tmp = tmp.next;
}

ListNode cur = null;
while (!queue.isEmpty()){
ListNode frist = queue.pollFirst();
ListNode last = queue.pollLast();
frist.next = last;
if (cur != null) {
cur.next = frist;
}
if (last != null) {
cur = last;
cur.next = null;
}
}
}
}