# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
# @param head ListNode类
# @return ListNode类
#
class Solution:
def deleteDuplicates(self , head ):
dummy = pre = ListNode(0)
dummy.next = head
cur = head
while cur and cur.next:
if cur.val == cur.next.val: # 若当前节点值与下一个节点值相同
while cur and cur.next and cur.val == cur.next.val:
cur = cur.next # 跳过相同的节点
cur = cur.next
pre.next = cur # 指向下一个正确的节点(跳过了之间若干个节点)
else:
cur = cur.next
pre = pre.next
return dummy.next
删除有序链表中重复出现的元素
最新推荐文章于 2022-05-09 17:57:40 发布