# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param head ListNode类
# @return ListNode类
#
class Solution:
def deleteDuplicates(self , head: ListNode) -> ListNode:
# write code here
if not head or not head.next:
return head
dummy = ListNode(-1001)
dummy.next = head#添加哑巴节点,确保头节点和之后的节点处于同样可操作状态
pre, cur = dummy, head#前一节点,当前节点
while cur:#当前节点存在
if cur.next and cur.val==cur.next.val:#当前节点与后一节点重复
while cur.next and cur.val==cur.next.val:#移动到重复节点最末尾节点
cur = cur.next
pre.next, cur = cur.next, cur.next#删除重复段
else:
pre, cur = cur, cur.next
return dummy.next