字典计数,数组转链表的解法
class Solution:
def deleteDuplication(self , pHead: ListNode) -> ListNode:
# write code here
if not pHead: return None
"""用字典记录链表每个元素出现次数"""
dic={}
curr=pHead
while curr:
dic[curr.val]=dic.get(curr.val,0)+1
curr=curr.next
"""选出出现次数为1的元素"""
ans=[]
for k,v in dic.items():
if v==1: ans.append(k)
return self.li2linkli(ans)
def li2linkli(self,li):
if not li: return None
self.head=ListNode(li[0])
if len(li)==1: return ListNode(li[0])
temp=self.head
for item in li[1:]:
item=ListNode(item)
temp.next=item
temp=temp.next
return self.head