当前位置:网站首页>Sword finger offer 35 Replication of complex linked list
Sword finger offer 35 Replication of complex linked list
2022-07-05 05:28:00 【ThE wAlkIng D】
Title Description
Problem analysis ( Use HashMap To do it )
1. Judge whether it is empty first , If it is empty, return null
2. newly build map Set to copy the original list to the new list
3. Create a pointer to the new linked list
Code instance
/* // 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;
Map<Node,Node> map = new HashMap<Node,Node>();
while(cur != null){
Map.put(cur,new Node(cur.val));
cur = cur.next;
}
cur = head;
while(cur != null){
Map.get(cur).next = Map.get(cur.next);
Map.get(cur).random = Map.get(cur.random);
cur = cur.next;
}
return Map.get(head);
}
}
边栏推荐
猜你喜欢
随机推荐
Add level control and logger level control of Solon logging plug-in
How can the Solon framework easily obtain the response time of each request?
Download xftp7 and xshell7 (official website)
剑指 Offer 06.从头到尾打印链表
MySQL数据库(一)
[转]: OSGI规范 深入浅出
Haut OJ 1316: sister choice buys candy III
Heap sort summary
Shell Sort
Acwing 4301. Truncated sequence
Csp-j-2020-excellent split multiple solutions
Remote upgrade afraid of cutting beard? Explain FOTA safety upgrade in detail
每日一题-搜索二维矩阵ps二维数组的查找
[turn]: Apache Felix framework configuration properties
记录QT内存泄漏的一种问题和解决方案
剑指 Offer 58 - II. 左旋转字符串
[interval problem] 435 Non overlapping interval
注解与反射
[trans]: spécification osgi
sync. Interpretation of mutex source code