-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPartitionList.java
More file actions
56 lines (49 loc) · 1.61 KB
/
PartitionList.java
File metadata and controls
56 lines (49 loc) · 1.61 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
51
52
53
54
55
56
package com.al;
import org.junit.jupiter.api.Test;
public class PartitionList {
@Test
public void test() {
Solution s = new Solution();
ListNode l = new ListNode(1);
l.next = new ListNode(4);
l.next.next = new ListNode(3);
l.next.next.next = new ListNode(2);
l.next.next.next.next = new ListNode(5);
l.next.next.next.next.next = new ListNode(2);
ListNode ans = s.partition(l, 3);
System.out.println("----------------");
while (ans!=null) {
System.out.println(ans.val);
ans = ans.next;
}
}
class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
class Solution {
public ListNode partition(ListNode head, int x) {
// add head node
ListNode firstPartHead = new ListNode(-1), secondPartHead = new ListNode(-1);
ListNode firstLoop = firstPartHead, secondLoop = secondPartHead;
ListNode tmp = new ListNode(-1);
while (head != null) {
tmp = head.next;
if (head.val < x) {
firstLoop.next = head;
firstLoop = firstLoop.next;
firstLoop.next = null;
} else {
secondLoop.next = head;
secondLoop = secondLoop.next;
secondLoop.next = null;
}
head = tmp;
}
secondLoop.next = null;
firstLoop.next = secondPartHead.next;
return firstPartHead.next;
}
}
}