题解 | #复杂链表的复制#
复杂链表的复制
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):
# write code here
cur = RandomListNode(-1) # 新链表工作指针
pre = RandomListNode(-1) # 新链表头结点
pre.next = cur
while pHead:
cur.next = RandomListNode(pHead.label) # 复制原链表的next域
cur = cur.next # 新链表指针往后移动
cur.random = pHead.random # 复制新链表的reandom域
pHead = pHead.next # 原链表指针往后移动
return pre.next.next
就可以直接复制啊,怎么还用到字典了,直接找到每个节点的next和random进行复制不就行了嘛~