题解 | #删除链表中重复的结点#
删除链表中重复的结点
http://www.nowcoder.com/practice/fc533c45b73a41b0b44ccba763f866ef
字典计数,数组转链表的解法
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
题解-数据结构与算法 文章被收录于专栏
小菜鸟的题解