[LeetCode]Partition List

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode partition(ListNode head, int x) {
        ListNode small = new ListNode(0);
        ListNode big = new ListNode(0);
        ListNode p = head;
        ListNode s = small;
        ListNode b = big;
        while (p != null) {
            if (p.val < x) {
                s.next = p;
                s = s.next;
                p = p.next;
                s.next = null;
            } else {
                b.next = p;
                b = b.next;
                p = p.next;
                b.next = null;
            }
        }
        s.next = big.next;
        return small.next;
    }
}

 

posted @ 2015-11-27 09:39  Weizheng_Love_Coding  阅读(101)  评论(0编辑  收藏  举报