-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPartition.java
More file actions
34 lines (29 loc) · 762 Bytes
/
Partition.java
File metadata and controls
34 lines (29 loc) · 762 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
33
34
import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;
/**
* 86. 分隔链表
*/
public class Partition {
public ListNode partition(ListNode head, int x) {
ListNode dummy = new ListNode(-1);
ListNode cur = dummy;
ListNode pos = head;
Queue<ListNode> queue = new LinkedBlockingQueue<>();
while (pos!=null){
if(pos.val<x){
cur.next = pos;
cur = cur.next;
}else{
queue.add(pos);
}
pos = pos.next;
}
while (!queue.isEmpty()){
cur.next = queue.poll();
cur = cur.next;
}
// 截断末尾
cur.next = null;
return dummy.next;
}
}