forked from hongtaocai/code_interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpartitionlist.java
More file actions
executable file
·40 lines (36 loc) · 930 Bytes
/
partitionlist.java
File metadata and controls
executable file
·40 lines (36 loc) · 930 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
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode partition(ListNode head, int x) {
// Start typing your Java solution below
// DO NOT write main() function
ListNode head1 = new ListNode(0);
ListNode head2 = new ListNode(0);
ListNode tail1 = head1;
ListNode tail2 = head2;
ListNode p = head;
while(p!=null){
if(p.val<x){
tail1.next = p;
tail1 = tail1.next;
}
else{
tail2.next = p;
tail2 =tail2.next;
}
p = p.next;
}
tail1.next = head2.next;
tail2.next = null;
return head1.next;
}
}