File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments