删除链表重复元素

https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/
一样,与之前作业一样的思路

class Solution:
    def deleteDuplicates(self, head: ListNode) -> ListNode:
        if head==None:return head
        rst=head#慢指针
        cur=head.next#快指针
        while cur:#当快指针不为None时
            if cur and head.val==cur.val:
                head.next=cur.next#head.next本来是指向cur的,为了实现删除,改为指向cur.next
                cur=cur.next#cur指针向后移
            else:
                head=cur#当不满足删除条件时,一起向后移
                cur=cur.next
        return rst

删除元素链接:https://blog.youkuaiyun.com/AliceRainly/article/details/104399090

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值