-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
executable file
·58 lines (46 loc) · 1.36 KB
/
Solution.java
File metadata and controls
executable file
·58 lines (46 loc) · 1.36 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
57
58
package $086;
import datastruc.ListNode;
/**
* @author Junlan Shuai[shuaijunlan@gmail.com].
* @date Created on 8:36 PM 2018/06/29.
*/
public class Solution {
public ListNode partition(ListNode head, int x) {
ListNode temp = new ListNode(0), pointer = temp, tail = temp;
while (head != null){
ListNode node = head;
if (head.val < x){
head = head.next;
node.next = pointer.next;
pointer.next = node;
pointer = node;
if (pointer.next == null){
tail = pointer;
}
}else {
head = head.next;
tail.next = node;
tail = node;
tail.next = null;
}
}
return temp.next;
}
public static void main(String[] args) {
ListNode head = new ListNode(1);
ListNode temp = head;
temp.next = new ListNode(4);
temp = temp.next;
temp.next = new ListNode(3);
temp = temp.next;
temp.next = new ListNode(2);
temp = temp.next;
temp.next = new ListNode(5);
temp = temp.next;
temp.next = new ListNode(2);
temp = temp.next;
Solution solution = new Solution();
solution.partition(head, 3);
System.out.println(1);
}
}