-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPartition.java
More file actions
33 lines (32 loc) · 926 Bytes
/
Partition.java
File metadata and controls
33 lines (32 loc) · 926 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
import java.util.LinkedList;
import java.util.Queue;
public class Partition {
public ListNode partition(ListNode pHead, int x) {
// write code here
if(pHead == null || pHead.next == null)
{
return pHead;
}
ListNode cur = pHead;
ListNode Shead = new ListNode(-1);
ListNode Bhead = new ListNode(-1);
ListNode Stmp = Shead;
ListNode Btmp = Bhead;
while(cur != null){
if(cur.val < x){
Stmp.next = new ListNode(cur.val);
Stmp = Stmp.next;
}else{
Btmp.next = new ListNode(cur.val);
Btmp = Btmp.next;
}
cur = cur.next;
}
cur = Shead;
while(cur.next != null && cur.next.val != -1){
cur = cur.next;
}
cur.next = Bhead.next;
return Shead.next;
}
}