思路:1.
2.
class Solution:
def deleteDuplicates(self, head: ListNode) -> ListNode:
if not head: # 1.head为空时,返回head
return head
rec = head
while head: # 2. head存在时:
nowval = head.val # 记录下此时head的值
while head.next and head.next.val == nowval: # 当head的下一个节点存在 并且 head值和head下一个节点值相等时
head.next = head.next.next # 删除head的下一个节点
head = head.next # 没有重复元素时(值不相等时,head后移)
return rec
原文:https://www.cnblogs.com/zzychage/p/15007978.html