-
Notifications
You must be signed in to change notification settings - Fork 3
/
_0086_PartitionList.java
53 lines (46 loc) · 1.59 KB
/
_0086_PartitionList.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
package com.diguage.algorithm.leetcode;
import com.diguage.algorithm.util.ListNode;
import java.util.Objects;
import static com.diguage.algorithm.util.ListNodeUtils.build;
import static com.diguage.algorithm.util.ListNodeUtils.printListNode;
import static java.util.Arrays.asList;
/**
* = 86. Partition List
*
* https://leetcode.com/problems/partition-list/[Partition List - LeetCode]
*
* @author D瓜哥, https://www.diguage.com/
* @since 2020-02-05 19:14
*/
public class _0086_PartitionList {
/**
* Runtime: 0 ms, faster than 100.00% of Java online submissions for Partition List.
* Memory Usage: 37.5 MB, less than 5.77% of Java online submissions for Partition List.
*/
public ListNode partition(ListNode head, int x) {
if (Objects.isNull(head)) {
return null;
}
ListNode dummySmall = new ListNode(0);
ListNode dummyLarge = new ListNode(0);
ListNode tailSmall = dummySmall;
ListNode tailLarge = dummyLarge;
while (Objects.nonNull(head)) {
if (head.val < x) {
tailSmall.next = head;
tailSmall = tailSmall.next;
} else {
tailLarge.next = head;
tailLarge = tailLarge.next;
}
head = head.next;
}
tailLarge.next = null;
tailSmall.next = dummyLarge.next;
return dummySmall.next;
}
public static void main(String[] args) {
_0086_PartitionList solution = new _0086_PartitionList();
printListNode(solution.partition(build(asList(1, 4, 3, 2, 5, 2)), 3));
}
}