[LeetCode] 83. Remove Duplicates from Sorted List

这道题虽然简单,但是需要注意的点还是比较多,首先是如果一直有重复的duplicate,要把所有的都去掉,所以inner loop是while而不是if,其次是每一层loop结束的条件,要搞清楚是head,还是head.next是不是None。

最后学习一下在python中如何建立一个class并且写出constructor

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def deleteDuplicates(self, head: ListNode) -> ListNode:
        if head is None:
            return head
        
        dummy = ListNode(0)
        dummy.next = head
        while head is not None:
            while head.next is not None and head.val == head.next.val:
                head.next = head.next.next
            head = head.next
        
        return dummy.next

 

posted on 2020-01-26 02:47  codingEskimo  阅读(80)  评论(0编辑  收藏  举报

导航