剑指offer 25. 复杂链表的复制

原题

输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序会直接返回空)

Reference Answer

解题思路:

剑指offer 25. 复杂链表的复制_第1张图片

这个题是分而治之的做法,很好玩。思想是在原来的链表每个节点后面都复制了一个同样的节点,再修改其指针,最后把偶数节点都抽出来,作为新的复杂链表。

剑指offer 25. 复杂链表的复制_第2张图片

剑指offer 25. 复杂链表的复制_第3张图片

剑指offer 25. 复杂链表的复制_第4张图片

# -*- 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
        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 = pNode.next
            pCloneNode = pCloneNode.next
            pNode.next = pCloneNode.next
            pNode = pNode.next
        return pCloneHead
            

C++ Version:

/*
struct RandomListNode {
    int label;
    struct RandomListNode *next, *random;
    RandomListNode(int x) :
            label(x), next(NULL), random(NULL) {
    }
};
*/
class Solution {
public:
    RandomListNode* Clone(RandomListNode* pHead)
    {
        CloneNodes(pHead);
        handleRandom(pHead);
        return ReconnectNodes(pHead);
    }
    
    void CloneNodes(RandomListNode* pHead){
        if (pHead == NULL){
            return;
        }
        RandomListNode* cloneHead = pHead;
        RandomListNode* cloneNode = cloneHead;
        RandomListNode* tempNode = NULL;
        while (cloneNode != NULL){
            tempNode = new RandomListNode(0);
            tempNode->label = cloneNode->label;
            tempNode->next = cloneNode->next;
            tempNode->random = NULL;
            
            cloneNode->next = tempNode;
            cloneNode = tempNode->next;
        }
    }
    
     //第二步,处理复杂指针的random
    void handleRandom(RandomListNode* pHead){
        RandomListNode* cloneNode = pHead;
        RandomListNode* tempNode = NULL;
        while (cloneNode != NULL){
            tempNode = cloneNode->next;
            
            if (cloneNode->random != NULL){
                tempNode->random = cloneNode->random->next;
            }
            
            cloneNode = tempNode->next;
        }
    }
    
        //第三步,拆分复杂指针
    RandomListNode* ReconnectNodes(RandomListNode* pHead){
        RandomListNode* pNode = pHead;
        RandomListNode* pClonedHead = NULL;
        RandomListNode* pClonedNode = NULL;
        
        if(pNode != NULL){
            pClonedHead = pClonedNode = pNode->next;
            pNode->next = pClonedNode->next;
            pNode = pNode->next;
        }
        
        while(pNode != NULL){
            pClonedNode->next = pNode->next;
            pClonedNode = pClonedNode->next;
            pNode->next = pClonedNode->next;
            pNode = pNode->next;
        }
        return pClonedHead;
    }
};

二刷:

# -*- 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 None
        
        head = pHead
        while head:
            node_next = head.next
            node = RandomListNode(head.label)
            head.next = node
            node.next = node_next
            head = node_next
            
        head = pHead
        while head:
            node_next = head.next
            if head.random:
                node_next.random = head.random.next
            head = node_next.next

        clone_head = None
        node = None
        if pHead:
            clone_head = pHead.next
            pHead.next = clone_head.next
            pHead = pHead.next
            node = clone_head
        while pHead:
            node.next = pHead.next
            node = node.next
            pHead.next = node.next
            pHead = pHead.next

        return clone_head

参考答案:https://blog.csdn.net/fuxuemingzhu/article/details/79622359

你可能感兴趣的:(剑指offer 25. 复杂链表的复制)