Skip to content

Instantly share code, notes, and snippets.

@thanlau
Created May 15, 2017 22:59
Show Gist options
  • Save thanlau/4c8b2587a2b0865dc46054e9173a423c to your computer and use it in GitHub Desktop.
Save thanlau/4c8b2587a2b0865dc46054e9173a423c to your computer and use it in GitHub Desktop.
/**
* Definition for ListNode.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int val) {
* this.val = val;
* this.next = null;
* }
* }
*/
public class Solution {
/**
* @param head: The first node of linked list.
* @param x: an integer
* @return: a ListNode
*/
public ListNode partition(ListNode head, int x) {
if(head == null){
return head;
}
ListNode dummy1 = new ListNode(0);
ListNode dummy2 = new ListNode(0);
ListNode left = dummy1, right = dummy2;
while(head !=null){
if(head.val< x){
left.next = head;
left = left.next;
}
else{
right.next = head;
right = right.next;
}
head = head.next;
}
right.next =null;
left.next = dummy2.next;
return dummy1.next;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment