[LeetCode]86. Partition List分离链表

/*
    这个题是medium的意思应该是用双指针的方法做,如果使用下边的新建链表的方法,就是easy的题目了
   双指针会用到很多链表的相连操作
     */
    public ListNode partition(ListNode head, int x) {
        ListNode s = null;
        ListNode b = null;
        ListNode temp=null,res=null;
        while (head!=null)
        {
            int cur = head.val;
            if (head.val<x)
            {
                if (s==null) {
                    s =  new ListNode(cur);
                     res = s;
                }
                else {
                    s.next = new ListNode(cur);
                    s = s.next;
                }
            }
            else
            {
                if (b==null) {
                    b =  new ListNode(cur);
                     temp = b;
                }
                else {
                    b.next = new ListNode(cur);
                    b = b.next;
                }
            }
            head = head.next;
        }
        if (s==null) return temp;
        s.next = temp;
        return res;
    }
原文地址:https://www.cnblogs.com/stAr-1/p/8431234.html