forked from yubinbai/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
55 lines (55 loc) · 1.55 KB
/
Solution.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
55
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}
public class Solution {
public ListNode partition(ListNode head, int x) {
ListNode curr = head;
if (curr == null) return null;
ListNode oldList = new ListNode(0);
ListNode oldListCurr = oldList;
ListNode newList = new ListNode(0);
ListNode newListCurr = newList;
while (curr != null) {
if (curr.val >= x) {
newListCurr.next = curr;
curr = curr.next;
newListCurr = newListCurr.next;
} else {
oldListCurr.next = curr;
curr = curr.next;
oldListCurr = oldListCurr.next;
}
}
oldListCurr.next = newList.next;
newListCurr.next = null;
return oldList.next;
}
public ListNode generateList(int[] list) {
ListNode head = new ListNode(0);
ListNode curr = head;
for (int i : list) {
curr.next = new ListNode(i);
curr = curr.next;
}
return head.next;
}
public void printList(ListNode e) {
while (e != null) {
System.out.format("%d\t", e.val);
e = e.next;
}
System.out.println("");
}
public static void main(String[] args) {
Solution sol = new Solution();
int[] intA = {1, 4, 3, 2, 5, 2};
ListNode in = sol.generateList(intA);
sol.printList(in);
sol.printList(sol.partition(in, 3));
}
}