-
Notifications
You must be signed in to change notification settings - Fork 1
/
PartitionList.java
68 lines (57 loc) · 1.71 KB
/
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package com.hncboy;
/**
* @author hncboy
* @date 2019/11/2 10:21
* @description 86.分隔链表
*
* 给定一个链表和一个特定值 x,对链表进行分隔,
* 使得所有小于 x 的节点都在大于或等于 x 的节点之前。
* 你应当保留两个分区中每个节点的初始相对位置。
*
* 示例:
* 输入: head = 1->4->3->2->5->2, x = 3
* 输出: 1->2->2->4->3->5
*/
public class PartitionList {
public static void main(String[] args) {
ListNode node = new ListNode(1);
node.next = new ListNode(4);
node.next.next = new ListNode(3);
node.next.next.next = new ListNode(2);
node.next.next.next.next = new ListNode(5);
node.next.next.next.next.next = new ListNode(2);
System.out.println(new PartitionList().partition(node, 3));
}
private ListNode partition(ListNode head, int x) {
if (head == null) {
return null;
}
ListNode highHead = new ListNode(-1);
// 存放比 x 大的节点的链表
ListNode high = highHead;
ListNode lowHead = new ListNode(-1);
// 存放比 x 小的节点的链表
ListNode low = lowHead;
while (head != null) {
if (head.val >= x) {
high.next = head;
high = high.next;
} else {
low.next = head;
low = low.next;
}
head = head.next;
}
// 拼接 low 和 high 链表
high.next = null;
low.next = highHead.next;
return lowHead.next;
}
private static class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
}