首页 > 其他 > 详细

[LeetCode]86. Partition List分离链表

时间:2018-02-08 14:06:48      阅读:188      评论:0      收藏:0      [点我收藏+]
/*
    这个题是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;
    }

 

[LeetCode]86. Partition List分离链表

原文:https://www.cnblogs.com/stAr-1/p/8431234.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!