-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpartition_list.java
More file actions
50 lines (45 loc) · 1.43 KB
/
partition_list.java
File metadata and controls
50 lines (45 loc) · 1.43 KB
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
public class Solution {
public ListNode partition(ListNode head, int x) {
// Start typing your Java solution below
// DO NOT write main() function
ListNode root = new ListNode(-1);
ListNode pivot = new ListNode(0);
ListNode root_last = root, pivot_last = pivot;
ListNode cur_node = head;
while (cur_node != null) {
//ListNode next = cur_node.next;
if (cur_node.val < x) {
root_last.next = cur_node;
root_last = cur_node;
} else {
pivot_last.next = cur_node;
pivot_last = cur_node;
//pivot_last.next = null;
}
cur_node = cur_node.next;
}
pivot_last.next = null;
root_last.next = pivot.next;
return root.next;
}
}
public ListNode partition(ListNode head, int x) {
ListNode root = new ListNode(-1);
ListNode pivot = new ListNode(x);
ListNode root_last = root, pivot_last = pivot;
ListNode cur_node = head;
while (cur_node != null) {
ListNode next = cur_node.next;
if (cur_node.val < x) {
root_last.next = cur_node;
root_last = cur_node;
} else {
pivot_last.next = cur_node;
pivot_last = cur_node;
pivot_last.next = null;
}
cur_node = next;
}
root_last.next = pivot.next;
return root.next;
}