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

复杂链表的复制

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


全部评论

相关推荐

牛客175617325号:有的面试官不开摄像头 可能是因为他是竞业来的
点赞 评论 收藏
分享
一个帅气潇洒的豆子:小公司
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务