[LeetCode] 83. Remove Duplicates from Sorted List

Posted codingEskimo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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

 

以上是关于[LeetCode] 83. Remove Duplicates from Sorted List的主要内容,如果未能解决你的问题,请参考以下文章

leetcode83-Remove Duplicates from Sorted List

[LeetCode] 83. Remove Duplicates from Sorted List

LeetCode83 Remove Duplicates from Sorted List

LeetCode 83 Remove Duplicates from Sorted List

LeetCode:83.Remove Duplicates from Sorted List

leetcode83 Remove Duplicates from Sorted List