LeetCode刷题--点滴记录011

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

要求

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

解题

C++版本
#include <iostream>
using namespace std;
#include <unordered_map>

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 head;
        Node* cur = head;
        unordered_map<Node*, Node*> map;
        
        while (cur != nullptr) {          //  复制各节点,并建立 “原节点 -> 新节点” 的 Map 映射
            map[cur] = new Node(cur->val);
            cur = cur->next;
        }
        cur = head;
        
        while (cur != nullptr) {           // 构建新链表的 next 和 random 指向
            map[cur]->next = map[cur->next];
            map[cur]->random = map[cur->random];
            cur = cur->next;
        }
        
        return map[head];          // 返回新链表的头节点
    }
};

void test01()
{
    Solution s;
    Node* node1 = new Node(7);
    Node* node2 = new Node(13);
    Node* node3 = new Node(11);
    Node* node4 = new Node(10);
    Node* node5 = new Node(1);

    node1->next = node2;
    node2->next = node3;
    node3->next = node4;
    node4->next = node5;

    node1->random = NULL;
    node2->random = node1;
    node3->random = node5;
    node4->random = node3;
    node5->random = node1;

    Node* copy = s.copyRandomList(node1);
    while(copy != NULL){
        if (copy->random != NULL)
        {
            cout << copy->val << " " << copy->random->val << " ";
        }
        else
            cout << copy->val << " ";
        copy = copy->next;
    }
    cout << endl;
}

int main()
{
    test01();

    system("pause");
    return 0;
}

在这里插入图片描述

Python版本
class Node:
    def __init__(self, x: int, next: 'Node' = None, random: 'Node' = None):
        self.val = int(x)
        self.next = next
        self.random = random
        
class Solution:
    def copyRandomList(self, head):
        if head==None: 
            return head
        dic = {}
        cur = head
        while cur: # 复制各节点,并建立 “原节点 -> 新节点” 的 Map 映射
            dic[cur] = Node(cur.val) # 创建字典对,原始节点一定为key
            cur = cur.next
        cur = head 
        
        while cur: # 构建新节点的 next 和 random 指向
            dic[cur].next = dic.get(cur.next)
            dic[cur].random = dic.get(cur.random)
            cur = cur.next
        
        return dic[head] # 返回新链表的头节点

def test01():
    solution = Solution()
    node = Node(7)
    node.next = Node(13)
    node.next.next = Node(11)
    node.next.next.next = Node(10)
    node.next.next.next.next = Node(1)
    
    node.random = None
    node.next.random = node
    node.next.next.random = node.next.next.next.next
    node.next.next.next.random = node.next.next
    node.next.next.next.next.random = node
    
    copy = solution.copyRandomList(node)
    while copy:
        if copy.random:
            print(copy.val,copy.random.val,end=' ')
        else:
            print(copy.val,end=' ')
        copy = copy.next
            
if __name__=="__main__":
    test01()

在这里插入图片描述

Java版本
package com.hailei_01;

import java.util.HashMap;
import java.util.Map;

public class copyRandomList {
    public static void main(String[] args) {
        Node node1 = new Node(7);
        Node node2 = new Node(13);
        Node node3 = new Node(11);
        Node node4 = new Node(10);
        Node node5 = new Node(1);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;

        node1.random = null;
        node2.random = node1;
        node3.random = node5;
        node4.random = node3;
        node5.random = node1;

        Node copy = copyRandomList(node1);
        while(copy != null)
        {
            if (copy.random != null)
            {
                System.out.print(copy.val+" "+copy.random.val+" ");
            }
            else
            {
                System.out.print(copy.val+" "+ null+" ");
            }
            copy = copy.next;
        }
    }

    public static class Node {
        int val;
        Node next, random;
        public Node(int val) {
            this.val = val;
            this.next = null;
            this.random = null;
        }
    }

    public static Node copyRandomList(Node head) {
        if(head == null) return null;
        Node cur = head;
        Map<Node, Node> map = new HashMap<>();

        while(cur != null) {  // 复制各节点,并建立 “原节点 -> 新节点” 的 Map 映射
            map.put(cur, new Node(cur.val));
            cur = cur.next;
        }
        cur = head;

        while(cur != null) {  // 构建新链表的 next 和 random 指向
            map.get(cur).next = map.get(cur.next);
            map.get(cur).random = map.get(cur.random);
            cur = cur.next;
        }

        return map.get(head); // 返回新链表的头节点
    }
}

在这里插入图片描述

希望本文对大家有帮助,上文若有不妥之处,欢迎指正

分享决定高度,学习拉开差距

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鲁棒最小二乘支持向量机

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值