题解 | #删除有序链表中重复的元素-II#
删除有序链表中重复的元素-II
https://www.nowcoder.com/practice/71cef9f8b5564579bf7ed93fbe0b2024?tpId=295&tqId=663&ru=/exam/oj&qru=/ta/format-top101/question-ranking&sourceUrl=%2Fexam%2Foj%3Fpage%3D1%26tab%3D%25E7%25AE%2597%25E6%25B3%2595%25E7%25AF%2587%26topicId%3D295
from os import XATTR_REPLACE # class ListNode: # def __init__(self, x): # self.val = x # self.next = None # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param head ListNode类 # @return ListNode类 # class Solution: def deleteDuplicates(self, head: ListNode) -> ListNode: # write code here print("******") if head == None: return None L = [] while head: L.append(head.val) head = head.next #在I的基础上将列表中出现次数大于1的元素排除在外,新建一个列表 L2=[] for x in L: if L.count(x) >1: continue else: L2.append(x) L2.sort() print(L) if not L2: return None head = ListNode(L2[0]) res = head for x in L2[1:]: node = ListNode(x) head.next = node head = head.next return res