首页 > 其他 > 详细

83. Remove Duplicates from Sorted List

时间:2018-09-24 19:25:30      阅读:155      评论:0      收藏:0      [点我收藏+]

一、题目

  1、审题

技术分享图片

  2、分析

    给出一个有序的有重复数值的整形链表,删除重复值的节点,使得每个节点的值只出现一次。

 

二、解答

  1、思路:

    同 eg 82,只是保留重复节点数值的一个节点

public ListNode deleteDuplicates(ListNode head) {
        if(head == null || head.next == null) 
            return head;
        
        ListNode fakeHead = new ListNode(0);
        fakeHead.next = head;
        ListNode pre = fakeHead;
        ListNode cur = head;
        
        while(cur != null) {
            while(cur.next != null && cur.next.val == cur.val)
                cur = cur.next;
            
            pre.next = cur;
            pre = cur;
            cur = cur.next;
        }
        
        return fakeHead.next;
    }

 

83. Remove Duplicates from Sorted List

原文:https://www.cnblogs.com/skillking/p/9696259.html

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