82. 删除排序链表中的重复元素 II

python
前面加个dummy节点,这样就不用考虑头节点的更新问题了

# 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:
        Head=ListNode('x')
        Head.next=head
        cur_number=head
        pre_number=Head
        while cur_number and cur_number.next:
            while cur_number and cur_number.next and cur_number.val==cur_number.next.val:
                cur_number=cur_number.next
            if pre_number.next!=cur_number:
                #有重复的
                pre_number.next=cur_number.next
                cur_number=pre_number.next
            else:
                #没重复的
                pre_number=cur_number
                cur_number=cur_number.next
        return Head.next
        
posted @ 2019-07-02 19:10  NeoZy  阅读(66)  评论(0编辑  收藏  举报