题解 | #复杂链表的复制#

复杂链表的复制

https://www.nowcoder.com/practice/f836b2c43afc4b35ad6adc41ec941dba

# -*- coding:utf-8 -*-
# class RandomListNode:
#     def __init__(self, x):
#         self.label = x
#         self.next = None
#         self.random = None
class Solution:
    # 返回 RandomListNode
    def Clone(self, pHead):
        # write code here
        if pHead is None:
            return pHead
        res = RandomListNode(0)
        map = dict()
        cur  = pHead
        pre = res
        while cur is not None:
            clone = RandomListNode(cur.label)
            map[cur] = clone
            pre.next = clone
            pre = pre.next
            cur = cur.next
        for (key,value) in map.items():
            if key.random is None:
                value.random = None
            else:
                value.random = map[key.random]
        return res.next


全部评论

相关推荐

不愿透露姓名的神秘牛友
06-30 18:19
点赞 评论 收藏
分享
qq乃乃好喝到咩噗茶:院校后面加上211标签,放大加粗,招呼语也写上211
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务