Skip to content

Instantly share code, notes, and snippets.

@Desolve
Created January 8, 2020 17:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Desolve/a4bf11639b8cd187f52e0d455277fe02 to your computer and use it in GitHub Desktop.
Save Desolve/a4bf11639b8cd187f52e0d455277fe02 to your computer and use it in GitHub Desktop.
0086 Partition List
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode partition(ListNode head, int x) {
ListNode d1 = new ListNode(0);
ListNode d2 = new ListNode(0);
ListNode ite1 = d1;
ListNode ite2 = d2;
while (head != null) {
ListNode nxt = head.next;
if (head.val < x) {
ite1.next = head;
ite1 = ite1.next;
ite1.next = null;
} else {
ite2.next = head;
ite2 = ite2.next;
ite2.next = null;
}
head = nxt;
}
if (d1.next == null)
return d2.next;
if (d2.next == null)
return d1.next;
ite1.next = d2.next;
return d1.next;
}
}
/* Simplify
class Solution {
public ListNode partition(ListNode head, int x) {
ListNode d1 = new ListNode(0);
ListNode d2 = new ListNode(0);
ListNode[] ite = {d1, d2};
while (head != null) {
ListNode nxt = head.next;
int i = head.val < x ? 0 : 1;
ite[i].next = head;
ite[i] = ite[i].next;
ite[i].next = null;
head = nxt;
}
if (d1.next == null) return d2.next;
if (d2.next == null) return d1.next;
ite[0].next = d2.next;
return d1.next;
}
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment