Created
May 15, 2017 22:59
-
-
Save thanlau/4c8b2587a2b0865dc46054e9173a423c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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