网络营销企业网站设计,网站基本建设投资内容,郑州pc网站开发,网站备份信息题目
给定一个已排序的链表的头 head #xff0c; 删除所有重复的元素#xff0c;使每个元素只出现一次 。返回 已排序的链表 。
代码
class Solution:def deleteDuplicates(self, head: Optional[ListNode]) - Optional[ListNode]:if head:# 判断非空链表current he…题目
给定一个已排序的链表的头 head 删除所有重复的元素使每个元素只出现一次 。返回 已排序的链表 。
代码
class Solution:def deleteDuplicates(self, head: Optional[ListNode]) - Optional[ListNode]:if head:# 判断非空链表current head # 定义一个指针遍历链表while current.next:if current.val current.next.val:# 指针不动只改变链表的部分指向if current.next.next: # 当前与下一个相等且下下个不为空则指向下下个current.next current.next.nextelse: # 当前与下一个相等且没有下下个则将当前指向空current.next Noneelse: # 如果当前与下一个不等则将指针往后移current current.nextreturn head