本问题对应的 leetcode 原文链接:剑指 Offer 35. 复杂链表的复制

问题描述

请实现 copyRandomList 函数,复制一个复杂链表。在复杂链表中,每个节点除了有一个 next 指针指向下一个节点,还有一个 random 指针指向链表中的任意节点或者 null。

示例 1:
img

输入:head = [[7,null],[13,0],[11,4],[10,2],[1,0]]
输出:[[7,null],[13,0],[11,4],[10,2],[1,0]]

示例 2:
img

输入:head = [[1,1],[2,1]]
输出:[[1,1],[2,1]]

示例 3:
img

输入:head = [[3,null],[3,0],[3,null]]
输出:[[3,null],[3,0],[3,null]]

示例 4:

输入:head = []
输出:[]
解释:给定的链表为空(空指针),因此返回 null。

提示:
-10000 <= Node.val <= 10000
Node.random 为空(null)或指向链表中的节点。
节点数目不超过 1000 。

代码实现

class Solution {
    public Node copyRandomList(Node head) {
        if(head == null){
            return null;
        }
        // 复制链表节点
        Node cur = head;
        while(cur != null){
            Node next = cur.next;
            cur.next = new Node(cur.val);
            cur.next.next = next;
            cur = next;
        }

        // 复制随机节点
        cur = head;
        while(cur != null){
            Node curNew = cur.next;
            curNew.random = cur.random == null ? null : cur.random.next;
            cur = cur.next.next;
        }

        // 拆分,比如把 A->A1->B->B1->C->C1拆分成 A->B->C和A1->B1->C1
        Node headNew = head.next;
        cur = head;
        Node curNew = head.next;
        while(cur != null){
            cur.next = cur.next.next;
            cur = cur.next;
            curNew.next = cur == null ? null : cur.next;
            curNew = curNew.next;
        }

        return headNew;
    }
}

时间复杂度:O(n)
额外空间复杂度:O(1)

Python

# Definition for a Node.
class Node:
    def __init__(self, x, next=None, random=None):
        self.val = int(x)
        self.next = next
        self.random = random

class Solution(object):
    def copyRandomList(self, head):
        """
        :type head: Node
        :rtype: Node
        """
        if not head:
            return None
        # 复制链表节点
        cur = head
        while cur:
            next = cur.next
            cur.next = Node(cur.val)
            cur.next.next = next
            cur = next

        # 复制随机节点
        cur = head
        while cur:
            curNew = cur.next
            curNew.random = cur.random.next if cur.random else None
            cur = cur.next.next

        # 拆分,比如把 A->A1->B->B1->C->C1拆分成 A->B->C和A1->B1->C1
        headNew = head.next
        cur = head
        curNew = head.next
        while cur:
            cur.next = cur.next.next
            cur = cur.next
            curNew.next = cur.next if cur else None
            curNew = curNew.next

        return headNew

Python

Copy

C++

/*
// Definition for a Node.
class Node {
public:
    int val;
    Node* next;
    Node* random;

    Node(int _val) {
        val = _val;
        next = NULL;
        random = NULL;
    }
};
*/
class Solution {
public:
    Node* copyRandomList(Node* head) {
        if (!head) {
            return nullptr;
        }
        // 复制链表节点
        Node* cur = head;
        while (cur) {
            Node* next = cur->next;
            cur->next = new Node(cur->val);
            cur->next->next = next;
            cur = next;
        }

        // 复制随机节点
        cur = head;
        while (cur) {
            Node* curNew = cur->next;
            curNew->random = cur->random ? cur->random->next : nullptr;
            cur = cur->next->next;
        }

        // 拆分,比如把 A->A1->B->B1->C->C1拆分成 A->B->C和A1->B1->C1
        Node* headNew = head->next;
        cur = head;
        Node* curNew = head->next;
        while (cur) {
            cur->next = cur->next->next;
            cur = cur->next;
            curNew->next = cur ? cur->next : nullptr;
            curNew = curNew->next;
        }

        return headNew;
    }
};

C++

Copy

Go

/**
 * Definition for a Node.
 * type Node struct {
 *     Val int
 *     Next *Node
 *     Random *Node
 * }
 */
func copyRandomList(head *Node) *Node {
    if head == nil {
        return nil
    }
    // 复制链表节点
    cur := head
    for cur != nil {
        next := cur.Next
        cur.Next = &Node{Val: cur.Val}
        cur.Next.Next = next
        cur = next
    }

    // 复制随机节点
    cur = head
    for cur != nil {
        curNew := cur.Next
        if cur.Random != nil {
            curNew.Random = cur.Random.Next
        }
        cur = cur.Next.Next
    }

    // 拆分,比如把 A->A1->B->B1->C->C1拆分成 A->B->C和A1->B1->C1
    headNew := head.Next
    cur = head
    curNew := head.Next
    for cur != nil {
        cur.Next = cur.Next.Next
        cur = cur.Next
        if cur == nil {
            curNew.Next = nil
        } else {
            curNew.Next = cur.Next
        }
        curNew = curNew.Next
    }

    return headNew
}

JS

/**
 * // Definition for a Node.
 * function Node(val, next, random) {
 *    this.val = val;
 *    this.next = next;
 *    this.random = random;
 * };
 */

/**
 * @param {Node} head
 * @return {Node}
 */
var copyRandomList = function(head) {
    if (head == null) {
        return null;
    }
    // 复制链表节点
    let cur = head;
    while(cur != null){
        let next = cur.next;
        cur.next = new Node(cur.val);
        cur.next.next = next;
        cur = next;
    }

    // 复制随机节点
    cur = head;
    while(cur != null){
        let curNew = cur.next;
        curNew.random = cur.random == null ? null : cur.random.next;
        cur = cur.next.next;
    }

    // 拆分,比如把 A->A1->B->B1->C->C1拆分成 A->B->C和A1->B1->C1
    let headNew = head.next;
    cur = head;
    let curNew = head.next;
    while(cur != null){
        cur.next = cur.next.next;
        cur = cur.next;
        curNew.next = cur == null ? null : cur.next;
        curNew = curNew.next;
    }

    return headNew;
};

发表回复

后才能评论

评论(2)

  • 普通 2023年7月9日 下午11:21
    /**
         *  剑指 Offer 35. 复杂链表的复制
         *  https://leetcode.cn/problems/fu-za-lian-biao-de-fu-zhi-lcof/
         *  思路:链表复制,借助hashMap完成
         */
        public Node copyRandomList(Node head) {
            Map<Node, Node> ansMap = new HashMap<>(); // key:原始结点 value:拷贝结点
            Node temp = head;
            // 1、将链表中的数据存储到map中
            while (temp != null) {
                ansMap.put(temp, new Node(temp.val));
                temp = temp.next;
            }
            temp = head;
            // 2、将拷贝结点相关属性进行链接
            while (temp != null) {
                ansMap.get(temp).next = ansMap.get(temp.next);
                ansMap.get(temp).random = ansMap.get(temp.random);
                temp = temp.next;
            }
            return ansMap.get(head);
        }
    
  • H、 普通 2023年7月8日 下午7:14

    先拼接,再拆分

    /*
    // Definition for a Node.
    class Node {
        int val;
        Node next;
        Node random;
    
        public Node(int val) {
            this.val = val;
            this.next = null;
            this.random = null;
        }
    }
    */
    class Solution {
        public Node copyRandomList(Node head) {
            if(head == null) return null;
            Node cur = head;
    
            while(cur != null) {
                Node tmp = new Node(cur.val);
                tmp.next = cur.next;
                cur.next = tmp;
                cur = tmp.next;
            }
    
            cur = head;
            while(cur != null) {
                if(cur.random != null)
                    cur.next.random = cur.random.next;
                cur = cur.next.next;
            }
    
            cur = head.next;
            Node pre = head, res = head.next;
            while(cur.next != null) {
                pre.next = pre.next.next;
                cur.next = cur.next.next;
                pre = pre.next;
                cur = cur.next;
            }
            pre.next = null;
            return res;     
        }
    }