美文网首页
82. Remove Duplicates from Sorte

82. Remove Duplicates from Sorte

作者: 羲牧 | 来源:发表于2020-05-26 08:31 被阅读0次

此题细节太多,非常适合用test-case driven的方法做题

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def deleteDuplicates(self, head: ListNode) -> ListNode:
        if head is None:
            return head
        H = ListNode()
        H.next = head
        p = H
        q = H.next
        r = H.next
        flag = False
        while r.next:
            r = r.next
            if r.val != q.val:
                q = q.next
                if flag:
                    if p == H:
                        H.next = q
                    p.next = q
                    flag = False
                else:
                    p = p.next
            else:
                flag = True
                q = q.next
        if flag:
            p.next = None
        return H.next
        
    ```

相关文章

网友评论

      本文标题:82. Remove Duplicates from Sorte

      本文链接:https://www.haomeiwen.com/subject/fttmahtx.html