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

复杂链表的复制

http://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):
        if pHead==None:
            return None

        # write code here
        cur=pHead
        head=RandomListNode(0)
        amap={}
        while(cur):
            new_node=RandomListNode(cur.label)
            amap[cur]=new_node
            cur=cur.next
        cur=pHead
        while (cur):
            node=amap[cur]
            node.next=amap[cur.next] if cur.next else None
            node.random=amap[cur.random] if cur.random else None
            cur=cur.next
        return amap[pHead]
全部评论

相关推荐

Wy_m:只要不是能叫的上名的公司 去实习没有任何意义 不如好好沉淀自己
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务