Old Wei wins the offer to take you to learn --- Brush title series (25 complex chain of copy)

25. The list of replication complex

problem:

A complex input list (each node has a node value, and two pointers, one pointing to the next node, a special pointer to any other node), returns a value after the head of the list replication complex. (Note that the output results do not return parameter node reference, otherwise the program will return empty sentenced questions directly)

thought:

Although this question is called the replication complex of the list, in fact, more than a new sub-links. We just need to create a data list node, then recursively copy of the original list.

python code:

# -*- 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(not pHead):
            return pHead
        p=RandomListNode(pHead.label)
        p.next=pHead.next
        p.random=pHead.random
        
        p.next=self.Clone(pHead.next)
        
        return p
Published 160 original articles · won praise 30 · views 70000 +

Guess you like

Origin blog.csdn.net/yixieling4397/article/details/104952333