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

题目:

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

示例 1:

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

示例 2:

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

示例 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 。

 

代码:

//主要难点是解决节点的random,利用两个动态数组,可以找到他们的random。

复制代码
 1 /*
 2 // Definition for a Node.
 3 class Node {
 4     int val;
 5     Node next;
 6     Node random;
 7 
 8     public Node(int val) {
 9         this.val = val;
10         this.next = null;
11         this.random = null;
12     }
13 }
14 */
15 class Solution {
16     public Node copyRandomList(Node head) {
17         if(head==null){
18            return null;
19         }
20 
21         else{
22             //利用两个动态数组存放新旧链表节点,这样可以利用旧动态数组下标找到新动态数组下标对应的节点
23             ArrayList<Node> arrayList0=new ArrayList<>();
24             ArrayList<Node> arrayList1=new ArrayList<>();
25 
26             Node he=head;
27             arrayList0.add(he);
28 
29             Node head_node=new Node(he.val);
30             Node node0=head_node;
31             arrayList1.add(node0);
32             //解决next
33             while(he.next!=null){
34                 Node node1=new Node(he.next.val);
35                 node0.next=node1;
36                 arrayList0.add(he.next);
37                 arrayList1.add(node1);
38                 node0=node1;
39                 he=he.next;
40             }
41             //解决random
42             node0=head_node;
43             he=head;
44             while(he!=null){
45                 if(he.random==null){
46                     node0.random=null;
47                     he=he.next;
48                     node0=node0.next;
49                 }else{
50                     int index=arrayList0.indexOf(he.random);
51                     node0.random=arrayList1.get(index);
52                     he=he.next;
53                     node0=node0.next;
54                 }
55             }
56             return head_node;
57         }
58     }
59 }
复制代码

 

 

代码2:

 

复制代码
 1 class Solution {
 2     public Node copyRandomList(Node head) {
 3         if(head==null){
 4            return null;
 5         }
 6 
 7         else{
 8             Map<Node,Node> map=new HashMap<>();
 9             Node cur=head;
10             //建立新旧链表的map映射
11             while(cur!=null){
12                map.put(cur,new Node(cur.val));
13                cur=cur.next;
14             }
15 
16             cur=head;
17             while(cur!=null){
18                 map.get(cur).next=map.get(cur.next);
19                 map.get(cur).random=map.get(cur.random);
20                 cur=cur.next;
21             }
22             return map.get(head);
23         }
24     }
25 }
复制代码

 

posted @   堤苏白  阅读(52)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示