码迷,mamicode.com
首页 > 其他好文 > 详细

Remove Duplicates from Sorted List II

时间:2015-04-08 12:24:47      阅读:95      评论:0      收藏:0      [点我收藏+]

标签:

和I 比起来其实很不一样,反而很类似于linkedlist reverse,不断的看pre.next的东西,我自己想了好久,开始用三个指针,完全糊涂,还是退回到原来的code,才明了

public class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        if(head==null||head.next==null) return head;
        ListNode h = new ListNode(-1);
        h.next = head;
        ListNode pre = h, cur = head; // 以cur 进行移动, pre 贯穿了弄好的list
        // 这里非常容易糊涂,看了自己以前的code才明白,不推荐什么flg,三个指针一起走
        while(cur!=null){
            while(cur.next!=null && pre.next.val==cur.next.val){
                cur =cur.next;
            }
            if(pre.next == cur) // node 相同,说明cur 没挪窝,也就是说要么cur.next==null || cur.next和cur的值不等
                {pre = pre.next;} // OK的cur编入pre的串里,这里 pre.next指向的node并不一定能编入pre的list里,见下行
            else
                {pre.next = cur.next;} // 没有判断好pre.next是否和后面的重复,所以在这里只是先把它“指向”要判断的位置
            cur = cur.next;
        }
        return h.next;
    }
}

 

Remove Duplicates from Sorted List II

标签:

原文地址:http://www.cnblogs.com/jiajiaxingxing/p/4401687.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!