leetcood学习笔记-83-删除链表中的重复元素

题目描述:

第一次提交:

class Solution:
    def deleteDuplicates(self, head: ListNode) -> ListNode:
        if head==None or head.next==None:
            return head
        head.next = self.deleteDuplicates(head.next)
            
        if head.val==head.next.val:
            head=head.next # python无需像C++一样手动释放内存
return head

方法二:

class Solution:
    def deleteDuplicates(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        lb = head
        # while写lb.val就会错?'NoneType' object has no attribute 'val'
        while lb and lb.next:
            if lb.val == lb.next.val:
                lb.next = lb.next.next
            else:
                lb = lb.next
        return head

 

posted @ 2019-03-13 19:19  oldby  阅读(108)  评论(0编辑  收藏  举报