给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字。
示例 1:
输入: 1->2->3->3->4->4->5 输出: 1->2->5
示例 2:
输入: 1->1->1->2->3 输出: 2->3
写得太渣渣
# Definition for singly- ed list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def deleteDuplicates(self, head):
\"\"\"
:type head: ListNode
:rtype: ListNode
\"\"\"
if head==None or head.next==None:#如果是空链表,或者只有一个节点的链表,直接返回
return head
#先处理头部,让第一个数字不是重复的
while head!=None and head.next!=None and head.val==head.next.val:
num=head.val
while head!=None and head.val==num:
head=head.next
if head==None or head.next==None or head.next.next==None:#如果变成空链表,或者只有一个、两个节点的链表,直接返回
return head
ptr=head.next#指向当前数字
preptr=head#指向上一个不同的数字
while ptr!=None and ptr.next!=None:
num=ptr.val
if ptr.next.val==num:
while ptr!=None and ptr.val==num:
preptr.next=ptr.next
ptr=ptr.next
else:
preptr=ptr
ptr=ptr.next
return head
继续阅读与本文标签相同的文章
下一篇 :
放置类游戏是否迎来了属于自己的“黄金时代”?
-
服务器远程的安全管理办法
2026-05-18栏目: 教程
-
语音顶会Interspeech 论文解读|Investigation of Transformer based Spelling Correction Model for CTC-based End-to-End Mandarin Speech Recognition
2026-05-18栏目: 教程
-
阿里云“网红"运维工程师白金:做一个平凡的圆梦人 | 9月11号栖夜读
2026-05-18栏目: 教程
-
十位大师零距离,云栖大会通票+限量周边,还不够诱人吗亲? | 开发者必读(062期)
2026-05-18栏目: 教程
-
相同类中方法间调用时日志Aop失效处理
2026-05-18栏目: 教程
