zoukankan      html  css  js  c++  java
  • leetcode82

     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建立链表
  • 相关阅读:
    左旋一个字符串和手摇反转法
    LCS
    游戏服务器学习_1
    面试题_带答案_2
    面试题_带答案
    安卓_13
    安卓_12activity
    安卓_12
    多盟_1
    安卓没删干净导致报错
  • 原文地址:https://www.cnblogs.com/asenyang/p/12018335.html
Copyright © 2011-2022 走看看