题目描述:
输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序会直接返回空)
解题方法
这个题是分而治之的做法,很好玩。思想是在原来的链表每个节点后面都复制了一个同样的节点,再修改其指针,最后把偶数节点都抽出来,作为新的复杂链表。
# -*- 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):
self.cloneNodes(pHead)
self.connectSiblingNodes(pHead)
return self.reconnectNodes(pHead)
def cloneNodes(self, pHead):
pNode = pHead
while pNode:
pCloned = RandomListNode(pNode.label)
pCloned.next = pNode.next
pNode.next = pCloned
pNode = pCloned.next
def connectSiblingNodes(self, pHead):
pNode = pHead
while pNode:
pclone = pNode.next
if pNode.random:
pclone.random = pNode.random.next
pNode = pclone.next
def reconnectNodes(self, pHead):
pNode = pHead
pCloneHead = None
pCloneNode = None
if pNode:
pCloneHead = pCloneNode = pNode.next
pNode.next = pCloneNode.next
pNode = pNode.next
while pNode:
pCloneNode.next, pCloneNode = pNode.next, pCloneNode.next
pNode.next, pNode = pCloneNode.next, pNode.next
return pCloneHead
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序会直接返回空)
方法一:
思路:
1. 把复制的结点链接在原始链表的每一对应结点后面
2. 把复制的结点的random指针指向被复制结点的random指针的下一个结点
3. 拆分成两个链表,奇数位置为原链表,偶数位置为复制链表,注意复制链表的最后一个结点的next指针不能跟原链表指向同一个空结点None,next指针要重新赋值None(判定程序会认定你没有完成复制)
代码:
# -*- 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 not pHead:
return None
dummy = pHead
# first step, N' to N next
while dummy:
dummynext = dummy.next
copynode = RandomListNode(dummy.label)
copynode.next = dummynext
dummy.next = copynode
dummy = dummynext
dummy = pHead
# second step, random' to random'
while dummy:
dummyrandom = dummy.random
copynode = dummy.next
if dummyrandom:
copynode.random = dummyrandom.next
dummy = copynode.next
# third step, split linked list
dummy = pHead
copyHead = pHead.next
while dummy:
copyNode = dummy.next
dummynext = copyNode.next
dummy.next = dummynext
if dummynext:
copyNode.next = dummynext.next
else:
copyNode.next = None
dummy = dummynext
return copyHead
方法二:递归
代码:
class Solution:
def Clone(self, head):
if not head: return
newNode = RandomListNode(head.label)
newNode.random = head.random
newNode.next = self.Clone(head.next)
return newNode
方法三:哈希表
代码:
class Solution:
def Clone(self, head):
nodeList = [] #存放各个节点
randomList = [] #存放各个节点指向的random节点。没有则为None
labelList = [] #存放各个节点的值
while head:
randomList.append(head.random)
nodeList.append(head)
labelList.append(head.label)
head = head.next
#random节点的索引,如果没有则为1
labelIndexList = map(lambda c: nodeList.index(c) if c else -1, randomList)
dummy = RandomListNode(0)
pre = dummy
#节点列表,只要把这些节点的random设置好,顺序串起来就ok了。
nodeList=map(lambda c:RandomListNode(c),labelList)
#把每个节点的random绑定好,根据对应的index来绑定
for i in range(len(nodeList)):
if labelIndexList[i]!=-1:
nodeList[i].random=nodeList[labelIndexList[i]]
for i in nodeList:
pre.next=i
pre=pre.next
return dummy.next