zoukankan      html  css  js  c++  java
  • 剑指offer:复杂链表的复制

    题目描述:

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

    解题思路:

    注意题目中括号里的话,直接返回输入的函数的参数,是会被判空的,还是老老实实的创建节点,做复制。

    思路一:

    先复制一次链表,将每个节点的next指针连接到下一节点。第二步再处理random指针。

    新旧链表同步扫描,针对每个节点i,在旧链表中判断其random指针是否为空,若不为空,保存其random指针所值节点r。再同步新旧链表从头开始扫描节点,在旧链表中找到与r相等的节点j,将此时新链表i这个位置节点的random指针指向新链表中j这个位置的节点。

    这个方法的需要两个循环,时间复杂度为O(n^2)。

    思路二:

    在第一次复制链表时,利用一个hash表,将每一个旧链表中的节点与新节点中的对应节点做一个映射。

    第二遍同步扫描新旧链表时,查询当前旧链表节点的random指针指向i节点,若不为空,则将新链表对应节点的random指针指向hash表hash[i]所映射的新链表中节点j。

    这个方法的通过辅助空间降低时间复杂度,时间复杂度为O(n)。

    代码:

    思路一:

    /*
    struct RandomListNode {
        int label;
        struct RandomListNode *next, *random;
        RandomListNode(int x) :
                label(x), next(NULL), random(NULL) {
        }
    };
    */
    class Solution {
    public:
        RandomListNode* Clone(RandomListNode* pHead)
        {
            if(pHead==nullptr)
                return pHead;
            RandomListNode* tmp = pHead;
            RandomListNode* cur = new RandomListNode(tmp->label);
            RandomListNode* nhead = cur;
            tmp = tmp->next;
            while(tmp!=nullptr)
            {
                RandomListNode* n = new RandomListNode(tmp->label);
                cur->next = n;
                cur = cur->next;
                tmp = tmp->next;
            }
            tmp = pHead;
            cur = nhead;
            RandomListNode *r, *head_of_ran, *nhead_of_ran;
            while(tmp!=nullptr && cur!=nullptr)
            {
                if(tmp->random!=nullptr)
                {
                    r = tmp->random;
                    head_of_ran = pHead;
                    nhead_of_ran = nhead;
                    while(head_of_ran != nullptr)
                    {
                        if(head_of_ran != r)
                        {
                            head_of_ran = head_of_ran->next;
                            nhead_of_ran = nhead_of_ran->next;
                        }
                        else
                        {
                            cur->random = nhead_of_ran;
                            break;
                        }
                    }
                }
                tmp = tmp->next;
                cur = cur->next;
            }
            return nhead;
            
        }
    };

    思路二:

    /*
    struct RandomListNode {
        int label;
        struct RandomListNode *next, *random;
        RandomListNode(int x) :
                label(x), next(NULL), random(NULL) {
        }
    };
    */
    class Solution {
    public:
        RandomListNode* Clone(RandomListNode* pHead)
        {
            if(pHead==nullptr)
                return pHead;
            map<RandomListNode*, RandomListNode*> OldtoNew;
            RandomListNode* tmp = pHead;
            RandomListNode* cur = new RandomListNode(tmp->label);
            RandomListNode* nhead = cur;
            OldtoNew[tmp] = cur;
            tmp = tmp->next;
            while(tmp!=nullptr)
            {
                RandomListNode* n = new RandomListNode(tmp->label);
                cur->next = n;
                cur = cur->next;
                OldtoNew[tmp] = cur;
                tmp = tmp->next;
            }
            tmp = pHead;
            cur = nhead;
            while(tmp!=nullptr)
            {
                if(tmp->random!=nullptr)
                {
                    cur->random = OldtoNew[tmp->random];
                }
                tmp = tmp->next;
                cur = cur->next;
            }
            return nhead;
            
        }
    };
  • 相关阅读:
    【leetcode】Recover Binary Search Tree
    【leetcode】Dungeon Game
    【leetcode】Text Justification
    【leetcode】Largest Number
    【leetcode】Merge k Sorted Lists
    【leetcode】Reverse Nodes in k-Group
    【leetcode】Multiply Strings
    【leetcode】Unique Binary Search Trees II
    hdu 1885 bfs+状压
    hdu 1429 bfs+状态压缩
  • 原文地址:https://www.cnblogs.com/LJ-LJ/p/10619832.html
Copyright © 2011-2022 走看看