-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPartitioinList.java
More file actions
40 lines (37 loc) · 1 KB
/
PartitioinList.java
File metadata and controls
40 lines (37 loc) · 1 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
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode partition(ListNode head, int x) {
ListNode dummy1 = new ListNode(0); // dummy node no 1
ListNode dummy2 = new ListNode(0); // dummy node no 2
ListNode prev1 = dummy1;
ListNode prev2 = dummy2;
ListNode current = head;
while(current != null)
{
if(current.val < x)
{
prev1.next = current;
prev1 = current;
}
else
{
prev2.next = current;
prev2 = current;
}
current = current.next;
}
prev2.next = null; // tail
prev1.next = dummy2.next;
head = dummy1.next;
return dummy1.next;
}
}