删除排序链表中的重复元素

给定一个排序链表,删除所有重复的元素使得每个元素只留下一个。

案例:

给定 1->1->2,返回 1->2

给定 1->1->2->3->3,返回 1->2->3

    public ListNode deleteDuplicates(ListNode head){
        if (head==null||head.next==null){
            return head;
        }
        ListNode pre = head;
        ListNode next = head.next;
        while (next!=null){
            if (pre.val==next.val){
                pre.next=next.next;
            }
            else {
                pre = next;
            }
            next=next.next;
        }
        return head;
    }
原文地址:https://www.cnblogs.com/bingo2-here/p/8864180.html