Skip to content

Commit af61999

Browse files
refactor 86
1 parent a9b790c commit af61999

File tree

1 file changed

+26
-28
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+26
-28
lines changed

src/main/java/com/fishercoder/solutions/_86.java

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,35 @@
66
* 86. Partition List
77
*
88
* 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.
1513
*/
1614
public class _86 {
1715

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;
3438
}
35-
head = head.next;
36-
}
37-
greater.next = null;
38-
less.next = right.next;
39-
return left.next;
4039
}
41-
}
4240
}

0 commit comments

Comments
 (0)