利用set()函数去重
set()去重时,会造成顺序发生变化,所以关键点在于,如何让去重之后的数组,按照之前链表的顺序排序
所以想着建立两个数组:一个是去重(顺序发生变化)的数组N,一个是不去重(copy链表)的数组n,按照n.index去对N排序,即可达到去重而且顺序不变的效果。
```# 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
N=set()
n=[]
while head is not None:
n.append(head.val)
N.add(head.val)
head=head.next
N0=list(N)
N0.sort(key=n.index)
N1=ListNode(-1)
temp=N1
for i in range(len(N0)):
#print(N0[i])
temp.next=ListNode(N0[i])
temp=temp.next
return N1.next