# def __init__(self, x):
# self.val = x
# self.next = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param pHead ListNode类
# @return ListNode类
#
class Solution:
def deleteDuplication(self , pHead: ListNode) -> ListNode:
# write code here
pre = ListNode(-1)
ppre = pre
pre.next = pHead
while pHead and pHead.next:
if pHead.val != pHead.next.val:
pre.next = pHead
pre = pre.next
pHead = pHead.next
else:
temp = pHead.val
while pHead and pHead.val == temp:
pHead = pHead.next
pre.next = pHead
return ppre.next
注意这里的关键是:在用while循环删除重复节点时,当退出循环时要让pre的下一个节点指向当前工作节点;大循环不遍历最后一个节点,小循环要遍历。