forked from mehul-1607/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_86.java
30 lines (28 loc) · 897 Bytes
/
_86.java
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
package com.fishercoder.solutions;
import com.fishercoder.common.classes.ListNode;
public class _86 {
public static class Solution1 {
public ListNode partition(ListNode head, int x) {
if (head == null || head.next == null) {
return head;
}
ListNode left = new ListNode(0);
ListNode right = new ListNode(0);
ListNode less = left;
ListNode greater = right;
while (head != null) {
if (head.val < x) {
less.next = head;
less = less.next;
} else {
greater.next = head;
greater = greater.next;
}
head = head.next;
}
greater.next = null;
less.next = right.next;
return left.next;
}
}
}