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

☆☆☆☆解法:本题同剑指56.删除链表中重复的结点

class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        if (head == null || head.next == null) return head;
        ListNode dummyHead = new ListNode(-1);
        dummyHead.next = head;
        ListNode pre = dummyHead;
        while (head != null && head.next != null) {
            if (head.val == head.next.val) {
                while (head.next != null && head.val == head.next.val) {
                    head = head.next;
                }
                head = head.next;
                pre.next = head;
            }else {
                pre = head;
                head = head.next;
            }
        }
        return dummyHead.next;
    }
}
原文地址:https://www.cnblogs.com/HuangYJ/p/14131907.html