1 import collections
 2 class Solution:
 3     def constructLink(self,lists):
 4         n = len(lists)
 5         if n == 0:
 6             return None
 7         if n == 1:
 8             return ListNode(lists[0])
 9         
10         head = ListNode(lists[-1])
11         for i in range(n-2,-1,-1):
12             cur = ListNode(lists[i])
13             cur.next = head 
14             head = cur
15         return head
16     
17     def deleteDuplicates(self, head: ListNode) -> ListNode:
18         dic = collections.OrderedDict()#使用有序字典
19         while head != None:
20             if head.val not in dic:
21                 dic[head.val] = 1
22             else:
23                 dic[head.val] += 1
24             head = head.next
25         lists = []
26         for k,v in dic.items():#保序
27             if v == 1:
28                 lists.append(k)#只保留出现一次的节点值
29         return self.constructLink(lists)#使用list建立链表

 

posted on 2019-12-10 18:19  Sempron2800+  阅读(219)  评论(0编辑  收藏  举报