题解 | #删除有序链表中重复的元素-II#
删除有序链表中重复的元素-II
https://www.nowcoder.com/practice/71cef9f8b5564579bf7ed93fbe0b2024
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param head ListNode类
# @return ListNode类
#
# 像这种题,在结束循环的时候一定要加一个 ans.next = None 以终止链表
class Solution:
def deleteDuplicates(self , head: ListNode) -> ListNode:
if head == None or head.next == None:
return head
preValue = head.val-1
ans = ListNode(0)
ansNode = ans
while head.next != None:
if head.val != preValue and head.val != head.next.val:
ans.next = head
ans = ans.next
preValue = head.val
head = head.next
if head.val != preValue:
ans.next = head
ans = ans.next
ans.next = None
return ansNode.next

