File tree 1 file changed +26
-28
lines changed
src/main/java/com/fishercoder/solutions
1 file changed +26
-28
lines changed Original file line number Diff line number Diff line change 6
6
* 86. Partition List
7
7
*
8
8
* Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.
9
-
10
- You should preserve the original relative order of the nodes in each of the two partitions.
11
-
12
- For example,
13
- Given 1->4->3->2->5->2 and x = 3,
14
- return 1->2->2->4->3->5.
9
+ * You should preserve the original relative order of the nodes in each of the two partitions.
10
+ * For example,
11
+ * Given 1->4->3->2->5->2 and x = 3,
12
+ * return 1->2->2->4->3->5.
15
13
*/
16
14
public class _86 {
17
15
18
- public static class Solution1 {
19
- public ListNode partition (ListNode head , int x ) {
20
- if (head == null || head .next == null ) {
21
- return head ;
22
- }
23
- ListNode left = new ListNode (0 );
24
- ListNode right = new ListNode (0 );
25
- ListNode less = left ;
26
- ListNode greater = right ;
27
- while (head != null ) {
28
- if (head .val < x ) {
29
- less .next = head ;
30
- less = less .next ;
31
- } else {
32
- greater .next = head ;
33
- greater = greater .next ;
16
+ public static class Solution1 {
17
+ public ListNode partition (ListNode head , int x ) {
18
+ if (head == null || head .next == null ) {
19
+ return head ;
20
+ }
21
+ ListNode left = new ListNode (0 );
22
+ ListNode right = new ListNode (0 );
23
+ ListNode less = left ;
24
+ ListNode greater = right ;
25
+ while (head != null ) {
26
+ if (head .val < x ) {
27
+ less .next = head ;
28
+ less = less .next ;
29
+ } else {
30
+ greater .next = head ;
31
+ greater = greater .next ;
32
+ }
33
+ head = head .next ;
34
+ }
35
+ greater .next = null ;
36
+ less .next = right .next ;
37
+ return left .next ;
34
38
}
35
- head = head .next ;
36
- }
37
- greater .next = null ;
38
- less .next = right .next ;
39
- return left .next ;
40
39
}
41
- }
42
40
}
You can’t perform that action at this time.
0 commit comments