86. Partition List

class Solution {
    public ListNode partition(ListNode head, int x) {
        ListNode l1=new ListNode(0);
        ListNode l2=new ListNode(0);
        ListNode pre=new ListNode(0);
        pre.next=head;
        ListNode p=pre,pl1=l1,pl2=l2;
        while(p!=null&&p.next!=null)
        {
            if(p.next.val<x)
            {
                pl1.next=p.next;
                pl1=pl1.next;
            }
            else
            {
                pl2.next=p.next;
                pl2=pl2.next;
            }
            p=p.next;
        }
        pl1.next=l2.next;
        pl2.next=null;
        return l1.next;
    }
}
原文地址:https://www.cnblogs.com/asuran/p/7604781.html