83. Remove Duplicates from Sorted List

一、题目

  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;
    }

 

posted @ 2018-09-24 19:08  skillking2  阅读(99)  评论(0编辑  收藏  举报