# class ListNode:
# 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
# 发现头节点不好处理的时候,我们可以添加一个头节点(哨兵节点)
if pHead == None:
return None
res = ListNode(0)
res.next = pHead
cur = res
while cur.next and cur.next.next :
#遇到相邻两个节点值相同情况时进行处理
if cur.next.val == cur.next.next.val:
temp = cur.next.val
# 将所有相同的都跳过,学习一下这种用while跳过的方法
while cur.next != None and cur.next.val == temp:
cur.next = cur.next.next
else:
cur = cur.next
return res.next
- 发现头节点不好处理的时候,我们可以添加一个头节点(哨兵节点)
- 学习一下这种用while跳过的方法