-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathSolution086.java
More file actions
40 lines (33 loc) · 795 Bytes
/
Solution086.java
File metadata and controls
40 lines (33 loc) · 795 Bytes
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
package algorithm.leetcode;
/**
* @author: mayuan
* @desc: 分隔链表
* @date: 2019/01/03
*/
public class Solution086 {
public ListNode partition(ListNode head, int x) {
ListNode node1 = new ListNode(0);
ListNode node2 = new ListNode(0);
ListNode p1 = node1, p2 = node2;
while (null != head) {
if (x > head.val) {
p1.next = head;
p1 = p1.next;
} else {
p2.next = head;
p2 = p2.next;
}
head = head.next;
}
p2.next = null;
p1.next = node2.next;
return node1.next;
}
private class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
}