链表 83. 删除排序链表中的重复元素
83. Remove Duplicates from Sorted List (Easy)
思路: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

浙公网安备 33010602011771号