[leetcode] 83. Remove Duplicates from Sorted List 解题报告

Given a sorted linked list, delete all duplicates such that each element appear only once.

For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.

备份头指针,分3种情况判断即可

public ListNode deleteDuplicates(ListNode head) {
        if (head==null || head.next==null) return head;
        ListNode index =head;
        while (index.next!=null){
            if (index.next.val==index.val && index.next.next!=null){
                index.next=index.next.next;
            }else if (index.next.val==index.val && index.next.next==null){
                index.next=null;
            }else {
                index=index.next;
            }
        }
        return head;
    }
原文地址:https://www.cnblogs.com/pulusite/p/7599853.html