题解 | #删除链表中重复的结点#

删除链表中重复的结点

https://www.nowcoder.com/practice/fc533c45b73a41b0b44ccba763f866ef?tpId=13&tqId=23450&ru=/exam/oj/ta&qru=/ta/coding-interviews/question-ranking&sourceUrl=%2Fexam%2Foj%2Fta%3Fpage%3D1%26tpId%3D13%26type%3D13

# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param pHead ListNode类
# @return ListNode类
#
class Solution:
    def deleteDuplication(self, pHead: ListNode) -> ListNode:
        # write code here
		
		'''
		遍历俩遍,将链表的值出现的次数存入哈希表,第二遍出现次数不等于1就删除
		'''
        cur = pHead
        Mydict = {}
        while cur:
            if cur.val not in Mydict.keys():
                Mydict[cur.val] = 1
                cur=cur.next
            else:
                Mydict[cur.val] += 1
                cur = cur.next

        newHead = ListNode(-1)
        newHead.next = pHead

        cur = newHead
        while cur.next:
            if Mydict[cur.next.val] != 1:
                cur.next = cur.next.next
            else:
                cur=cur.next
        return newHead.next

全部评论

相关推荐

把球:这个听过,你加了就会发现是字节的hr
点赞 评论 收藏
分享
程序员猪皮:看不到八股什么意思
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务