(链表) 剑指 Offer 35. 复杂链表的复制 ——【Leetcode每日一题】

❓ 剑指 Offer 35. 复杂链表的复制

难度:中等

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

示例 1:

(链表) 剑指 Offer 35. 复杂链表的复制 ——【Leetcode每日一题】_第1张图片

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

示例 2:

(链表) 剑指 Offer 35. 复杂链表的复制 ——【Leetcode每日一题】_第2张图片

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

示例 3:

(链表) 剑指 Offer 35. 复杂链表的复制 ——【Leetcode每日一题】_第3张图片

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

示例 4:

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

提示

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

注意:本题与 138. 复制带随机指针的链表 相同。

思路:迭代 + 节点拆分

例如:
(链表) 剑指 Offer 35. 复杂链表的复制 ——【Leetcode每日一题】_第4张图片

第一步,在每个节点的后面插入复制的节点
(链表) 剑指 Offer 35. 复杂链表的复制 ——【Leetcode每日一题】_第5张图片

第二步,对复制节点的 random 链接进行赋值。
(链表) 剑指 Offer 35. 复杂链表的复制 ——【Leetcode每日一题】_第6张图片

第三步,拆分。
(链表) 剑指 Offer 35. 复杂链表的复制 ——【Leetcode每日一题】_第7张图片

代码:(C++、Java)

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 == nullptr) return nullptr;
        //第一步,复制
        Node* cur = head;
        while(cur != nullptr){
            Node* node = new Node(cur->val);
            node->next = cur->next;
            cur->next = node;
            cur = node->next;
        }
        //第二步,对random赋值
        cur = head;
        while(cur != nullptr){
            cur->next->random = cur->random == nullptr ? nullptr : cur->random->next;
            cur = cur->next->next;
        }
        //第三步,拆分
        cur = head;
        Node* newHead = head->next;
        while(cur->next != nullptr ){
            Node* temp = cur->next;
            cur->next = temp->next;
            cur = temp;
        }
        return newHead;
    }
};

Java

/*
// 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 node = new Node(cur.val);
            node.next = cur.next;
            cur.next = node;
            cur = node.next;
        }
        //第二步,对random赋值
        cur = head;
        while(cur != null){
            cur.next.random = cur.random == null ? null : cur.random.next;
            cur = cur.next.next;
        }
        //第三步,拆分
        cur = head;
        Node newHead = head.next;
        while(cur.next != null){
            Node temp = cur.next;
            cur.next = temp.next;
            cur = temp;
        }
        return newHead;
    }
}

运行结果:

(链表) 剑指 Offer 35. 复杂链表的复制 ——【Leetcode每日一题】_第8张图片

复杂度分析:

  • 时间复杂度 O ( n ) O(n) O(n),其中 n 是链表的长度。我们只需要遍历该链表三次。
  • 空间复杂度 O ( 1 ) O(1) O(1)

题目来源:力扣。

放弃一件事很容易,每天能坚持一件事一定很酷,一起每日一题吧!
关注我LeetCode主页 / CSDN—力扣专栏,每日更新!

注: 如有不足,欢迎指正!

你可能感兴趣的:(LeetCode,链表,leetcode,数据结构)