面试题26:复杂链表的复制

面试题26:复杂链表的复制

题目描述:
输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点)。

题目分析:

本质上该题目考察的是将复杂问题分解成简单问题的能力。

复杂链表,顾名思义,相对于普通链表会复杂一些。复制链表,首先考虑如果是普通链表如很容易复制,复杂链表复制在于链表结点中有一个特殊指针可指向任意一个结点,复制特殊指针时需要遍历当前链表找到指向结点的指针。

1. 朴素解法

思路1:
第一步:按照普通的单链表复制生成新的链表;
第二步:修改新的链表中每个结点的random域;
由于random域在原链表中指向第几个,则在复制后的新链表中也要指向第几个,修改每一个结点的random域都需要遍历一遍原链表和新链表,所以时间复杂度是O(N^2)。
代码如下:

/*
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 == 0)
            return 0;
        RandomListNode *res, *mov, *head, *cur;
        head = pHead;
        res = CreateList(head);
        mov = res;
        cur = pHead;
        while (mov) {
            RandomListNode *step1, *step2;
            step1 = res;
            step2 = pHead;
            while (cur->random != step2) {
                step2 = step2->next;
                step1 = step1->next;
            }
            mov->random = step1;
            mov = mov->next;
            cur = cur->next;
        }
        return res;
    }
    RandomListNode *CreateList(RandomListNode *head) {
        RandomListNode *newhead = new RandomListNode(head->label);
        head = head->next;
        RandomListNode *mov = newhead;
        while (head) {
            RandomListNode *node = new RandomListNode(head->label);
            mov->next = node;
            head = head->next;
            mov = mov->next;
        }    
        return newhead;
    }
};

2. 用空间换取时间的优化

思路2:
思路1中时间主要花费在寻找random域指向的结点,所以我们可以在第一步时,用哈希表保存

3. 不用辅助空间

思路3:
不用辅助空间的情况下O(N)的时间复杂度的算法。
第一步:按但链表的复制方式复制,但将新产生的结点N’连接在原链表N之后;
第二步:修改random域,如修改S’的random域,S的random域指向N,则S’的random域指向N’,N’在N的后面,则

s->random = s->random->next;

第三步:拆分两个链表;
《剑指offer》代码如下:

class Solution {
public:
    RandomListNode* Clone(RandomListNode* pHead)
    {
        if (pHead == 0)
            return 0;
        RandomListNode *res;
        CreateList(pHead);
        ModifyRandom(pHead);
        res = SplitList(pHead);
        return res;
    }
    void CreateList(RandomListNode *pHead) {
        RandomListNode *head = pHead;
        while (head) {
            RandomListNode *node = new RandomListNode(head->label);
            node->next = head->next;
            head->next = node;
            head = node->next;
        }    
    }
    void ModifyRandom(RandomListNode *head) {
        RandomListNode *cur = head;
        while (cur) {
            RandomListNode *clone = cur->next;
            if (cur->random) {
                clone->random = cur->random->next;
            }
            cur = clone->next;
        }
    }
    RandomListNode *SplitList(RandomListNode *head) {
        RandomListNode *pnode, *clone_head, *clone_node;
        pnode = head;
        clone_head = 0;
        clone_node = 0;
        if (pnode) {
            clone_head = clone_node = pnode->next;
            pnode->next = clone_node->next;
            pnode = pnode->next;
        }
        while (pnode) {
            clone_node->next = pnode->next;
            clone_node = clone_node->next;
            pnode->next = clone_node->next;
            pnode = pnode->next;
        }
        return clone_head;
    }
};

更好的代码:

/*
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 == 0)
            return 0;
        RandomListNode *cur = pHead;
        while (cur) {
            RandomListNode *node = new RandomListNode(cur->label);
            node->next = cur->next;
            cur->next = node;
            cur = node->next;
        }    
        cur = pHead;
        while (cur) {
            RandomListNode *clone = cur->next;
            if (cur->random) {
                clone->random = cur->random->next;
            }
            cur = clone->next;
        }
        /* 
         * 拆分两个链表,充分考察链表的熟练程度
         */
        cur = pHead;
        RandomListNode *clone_head = pHead->next;
        RandomListNode *pnext = cur->next;
        while (cur->next) {
            pnext = cur->next;
            cur->next = pnext->next;
            cur = pnext;
        }
        return clone_head; 
    }
};

另外一种解法:注意最后一个结点的处理,不能访问空指针的next。

class Solution {
public:
    RandomListNode* Clone(RandomListNode* pHead)
    {
        if (pHead == NULL)
            return NULL;
        //copy link list and append next
        CloneNodes(pHead);
        //mend link list
        ConnectRandomNodes(pHead);
        //split list
        RandomListNode *ret = SplitRandomList(pHead);
        return ret;
    }
    void CloneNodes(RandomListNode *pHead) {
        RandomListNode *head = pHead;
        while (head != NULL) {
            RandomListNode *node = new RandomListNode(head->label);
            node->next = head->next;
            head->next = node;
            head = node->next;
        }
    }
    void ConnectRandomNodes(RandomListNode *pHead) {
        RandomListNode *head = pHead;
        while (head != NULL) {
            RandomListNode *clone = head->next;
            if (head->random != NULL)
                clone->random = head->random->next;
            head = clone->next;
        }
    }
    RandomListNode *SplitRandomList(RandomListNode *pHead) {
        RandomListNode *head = pHead;
        RandomListNode *ret = head->next;
        RandomListNode *clone = NULL;
        while (head != NULL) {
            clone = head->next;
            head->next = clone->next;
            //注意对空指针的处理
            if (head->next != NULL)
                clone->next = head->next->next;
            else
                clone->next = NULL;
            head = head->next;
        }

        return ret;
    }
};
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值