题解 | #复杂链表的复制#

复杂链表的复制

https://www.nowcoder.com/practice/f836b2c43afc4b35ad6adc41ec941dba

# -*- 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 pHead is None:
            return pHead
        cur = pHead
        # 创建新的链表节点,并且将其与原链表连接到一起 
        # 此时随机结点还没有new出来,不能连接
        # 此种方法需要遍历三次链表 第一次n 第二次第三次2n 时间复杂度为o(n)
        # 但是空间复杂度为o(n)
        while cur:
            clone = RandomListNode(cur.label)
            clone.next = cur.next
            cur.next = clone
            cur = clone.next
        # 连接随机结点
        # 初始化结点位置
        cur = pHead
        clone = cur.next
        res = cur.next
        while cur:
            # 末尾结点不能为空
            if cur.random == None:
                clone.random = None
                cur = clone.next
                if cur != None:
                    clone = cur.next
            else:
                # cur的next就是重新生成的结点
                clone.random = cur.random.next
                cur = clone.next
                if cur != None:
                    clone = cur.next
        #  开始断链
        cur = pHead
        clone = cur.next
        while cur:
            # cur.next 必定不为空
            cur.next = clone.next
            cur = cur.next
            # 判断cur是否为None -> 检查末尾
            if cur is not None:
                clone.next = cur.next
                clone = clone.next
        return res

全部评论

相关推荐

群星之怒:不是哥们,你就不好奇瘫痪三十年的老植物人是啥样的吗?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务