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

复杂链表的复制

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
    nodeMap = {}
    def Clone(self, pHead):
        # write code here
        #递归,去处理每一个label,同时使用一个map维护当前带有xlabel的节点是否已经处理过了
        
        if pHead == None :
            return None
        
        if pHead.label in Solution.nodeMap:
            return Solution.nodeMap[pHead.label]
        else:
            #处理当前label
            currentNode = RandomListNode(pHead.label)
            Solution.nodeMap[pHead.label] = currentNode
            
            currentNode.next = self.Clone( pHead.next)
            currentNode.random = self.Clone(pHead.random)
            return currentNode
全部评论

相关推荐

点赞 评论 收藏
分享
墨西哥大灰狼:如果你的校友卤馆还在的话,他肯定会给你建议的,可是卤馆注销了@ 程序员卤馆
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务