leetcode Partition List

题目链接

package newcoder;

class ListNode {
      int val;
      ListNode next;
      ListNode(int x) { val = x; }
}

public class Solution {
    public static void main(String args[])
    {
        Solution
        obj=new Solution();
        ListNode h=new ListNode(2);

        h.next=new ListNode(1);
        obj.partition(h, 1);

    }


    public ListNode partition(ListNode head, int x) {
        if(head==null)
        {
            return head;
        }
        ListNode result=null;
        ListNode current=head;
        ListNode bigList=null;


        ListNode resultTal=null;
        ListNode bigTal=null;
        if(head.val>=x)
        {
            bigList=head;
            while(current.next!=null&&current.next.val>=x)
            {

                    current=current.next;

            }
            resultTal=result=current.next;
            bigTal=current;
        }
        else
        {
            result=head;
            while(current.next!=null&&current.next.val<x)
            {

                    current=current.next;

            }
            bigTal=bigList=current.next;
            resultTal=current;
        }
        if(current.next!=null)//current.next 已经处理
        {
            current=current.next.next;
        }
        else
        {
            current=null;
        }


        while(current!=null)
        {
            if(current.val<x)
            {
                resultTal.next=current;
                resultTal=current;
            }
            else
            {
                bigTal.next=current;
                bigTal=current;
            }
            current=current.next;
        }
        if(bigTal!=null)
        {
            bigTal.next=null;
        }
        if(result==null)
        {
            return bigList;
        }
        resultTal.next=bigList;
        return result;
    }
}
阅读更多
个人分类: leetCode
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

leetcode Partition List

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭