/**
* 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) {
// write your code here
ListNode leftDummy = new ListNode(-1);
ListNode rightDummy = new ListNode(-1);
ListNode leftHead = leftDummy;
ListNode rightHead = rightDummy;
while (head != null) {
if (head.val < x) {
leftHead.next = head;
leftHead = leftHead.next;
} else {
rightHead.next = head;
rightHead = rightHead.next;
}
head = head.next;
}
leftHead.next = rightDummy.next;
rightHead.next = null; //Attn!!!!!
return leftDummy.next;
}
}
2016年9月11日星期日
[LintCode] #96 Partition List
订阅:
博文评论 (Atom)
没有评论:
发表评论