题解 | #复杂链表的复制#
复杂链表的复制
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 None
# 定义个字典用于返回值
dict = {}
# 定义一个临时结点
cur = pHead
# 将字典结点化
while cur:
dict[cur] = RandomListNode(cur.label)
cur = cur.next
# 字典next,random指针开始指向对应的值
cur = pHead
while cur:
dict[cur].next = dict.get(cur.next)
dict[cur].random = dict.get(cur.random)
cur = cur.next
return dict[pHead]
#23届找工作求助阵地#