-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2_4.java
54 lines (51 loc) · 1.26 KB
/
2_4.java
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import java.util.*;
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Partition {
public ListNode partition(ListNode pHead, int x) {
// write code here
ListNode beforeBg = null, beforeEd = null,
afterBg = null,afterEd = null;
while(pHead != null)
{
ListNode next = pHead.next;
pHead.next = null;
if(pHead.val <x)
{
if(beforeBg == null)
{
beforeBg = pHead;
beforeEd = pHead;
}
else
{
beforeEd.next = pHead;
beforeEd = pHead;
}
}
else
{
if(afterBg == null)
{
afterBg = pHead;
afterEd = pHead;
}
else
{
afterEd.next = pHead;
afterEd = pHead;
}
}
pHead = next;
}
if(beforeBg == null) return afterBg;
beforeEd.next = afterBg;
return beforeBg;
}
}