Skip to content

Instantly share code, notes, and snippets.

@kelly-us
Created April 13, 2015 05:51
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 kelly-us/7676cd5ed4cff60cadab to your computer and use it in GitHub Desktop.
Save kelly-us/7676cd5ed4cff60cadab to your computer and use it in GitHub Desktop.
public ListNode splitList(ListNode head, int x){
if(head == null) return null;
ListNode before = null;
ListNode after = null;
//partition list
ListNode cur = head;
while(cur != null){
ListNode next = cur.next;
if(cur.data < x){
cur.next = before;
before = cur;
}
else{
cur.next = after;
after = cur;
}
cur = next;
}
//Missing
if(before == null) return after;
//merge
head = before;
while(before.next != null){
before = before.next;
}
before.next = after;
return head;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment