[leetcode] 82. 删除排序链表中的重复元素 II

本文介绍了一种解决LeetCode上82题的方法:删除排序链表中的重复元素II。这是83题的扩展版,通过引入额外的指针last_last来跟踪重复元素前的节点,从而更有效地处理连续重复元素的情况。

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

83题的扩展,相比于83题。我们增加一个last_last来记录重复元素前面的节点即可

class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        if (head == null) return head;
        if (head.next == null) return head;
        ListNode now = head.next;
        ListNode last = head;
        ListNode last_last = null;
        while (now != null) {
            if (now.val == last.val) {
                while (now != null && now.val == last.val) {
                    now = now.next;
                }
                if (last_last != null) last_last.next = now;
                else {
                    head = now;
                }
            } else
                last_last = last;
            last = now;
            if (now != null)
                now = now.next;
        }
        return head;
    }
}

转载于:https://www.cnblogs.com/acbingo/p/9391545.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值