首页 > 其他 > 详细

leetcode82

时间:2019-12-10 18:37:54      阅读:65      评论:0      收藏:0      [点我收藏+]
 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建立链表

 

leetcode82

原文:https://www.cnblogs.com/asenyang/p/12018335.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!