Skip to content

Commit 86d5e48

Browse files
authored
Create 86. Partition List
1 parent 7f74bdf commit 86d5e48

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

86. Partition List

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode() {}
7+
* ListNode(int val) { this.val = val; }
8+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9+
* }
10+
*/
11+
class Solution {
12+
public ListNode partition(ListNode head, int x) {
13+
ListNode small = new ListNode(0);
14+
ListNode higher = new ListNode(0);
15+
16+
ListNode smallHead=small, higherHead = higher;
17+
18+
while(head!=null){
19+
if(head.val<x){
20+
//small list
21+
smallHead.next = head;
22+
smallHead = smallHead.next;
23+
}
24+
else{
25+
//high list
26+
higherHead.next = head;
27+
higherHead = higherHead.next;
28+
}
29+
head=head.next;
30+
}
31+
32+
higherHead.next = null;
33+
smallHead.next = higher.next;
34+
35+
return small.next;
36+
37+
}
38+
}

0 commit comments

Comments
 (0)