当前位置:网站首页>Leetcode (Sword finger offer) - 35 Replication of complex linked list
Leetcode (Sword finger offer) - 35 Replication of complex linked list
2022-07-04 09:20:00 【Programmer Mu code】
Topic link : Click to open the link
The main idea of the topic : A little .
Their thinking : Solution (2) analysis .
Related enterprises
- Bytes to beat
- Amazon (Amazon)
- Microsoft (Microsoft)
- Bloomberg (Bloomberg)
- Google (Google)
- Shopee
- VMware
AC Code
- 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; } } */
// Solution (1)
class Solution {
public Node copyRandomList(Node head) {
Node node = head;
Map<Node, Node> map = new HashMap<>();
while (null != node) {
map.put(node, new Node(node.val));
node = node.next;
}
node = head;
while (null != node) {
Node tmp = map.get(node);
tmp.next = map.get(node.next);
tmp.random = map.get(node.random);
node = node.next;
}
return map.get(head);
}
}
// Solution (2)
class Solution {
public Node copyRandomList(Node head) {
if(head == null) return null;
Node cur = head;
// 1. Copy each node , And build a splicing linked list
while(cur != null) {
Node tmp = new Node(cur.val);
tmp.next = cur.next;
cur.next = tmp;
cur = tmp.next;
}
// 2. Build the of each new node random Point to
cur = head;
while(cur != null) {
if(cur.random != null)
cur.next.random = cur.random.next;
cur = cur.next.next;
}
// 3. Split two linked lists
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; // Handle the original end node of the linked list separately
return res; // Return the new chain header node
}
}
- C++
// Solution (1)
class Solution {
public:
Node* copyRandomList(Node* head) {
if(head == nullptr) return nullptr;
Node* cur = head;
unordered_map<Node*, Node*> map;
// 3. Copy each node , And establish “ Original node -> New node ” Of Map mapping
while(cur != nullptr) {
map[cur] = new Node(cur->val);
cur = cur->next;
}
cur = head;
// 4. To build a new linked list next and random Point to
while(cur != nullptr) {
map[cur]->next = map[cur->next];
map[cur]->random = map[cur->random];
cur = cur->next;
}
// 5. Return the head node of the new linked list
return map[head];
}
};
// Solution (2)
class Solution {
public:
Node* copyRandomList(Node* head) {
if(head == nullptr) return nullptr;
Node* cur = head;
// 1. Copy each node , And build a splicing linked list
while(cur != nullptr) {
Node* tmp = new Node(cur->val);
tmp->next = cur->next;
cur->next = tmp;
cur = tmp->next;
}
// 2. Build the of each new node random Point to
cur = head;
while(cur != nullptr) {
if(cur->random != nullptr)
cur->next->random = cur->random->next;
cur = cur->next->next;
}
// 3. Split two linked lists
cur = head->next;
Node* pre = head, *res = head->next;
while(cur->next != nullptr) {
pre->next = pre->next->next;
cur->next = cur->next->next;
pre = pre->next;
cur = cur->next;
}
pre->next = nullptr; // Handle the original end node of the linked list separately
return res; // Return the new chain header node
}
};
边栏推荐
- Reload CUDA and cudnn (for tensorflow and pytorch) [personal sorting summary]
- Report on research and investment prospect prediction of China's electronic grade sulfuric acid industry (2022 Edition)
- Educational Codeforces Round 115 (Rated for Div. 2)
- C语言-入门-基础-语法-[变量,常亮,作用域](五)
- Sword finger offer 30 contains the stack of Min function
- Lauchpad X | 模式
- After unplugging the network cable, does the original TCP connection still exist?
- Reading notes of how the network is connected - understanding the basic concepts of the network (I)
- Basic data types in golang
- Nurse level JDEC addition, deletion, modification and inspection exercise
猜你喜欢
C language - Introduction - Foundation - syntax - [identifier, keyword, semicolon, space, comment, input and output] (III)
165 webmaster online toolbox website source code / hare online tool system v2.2.7 Chinese version
Explain TCP protocol in detail three handshakes and four waves
Sequence model
Logstack configuration details -- elasticstack (elk) work notes 020
How to batch change file extensions in win10
C language - Introduction - Foundation - syntax - data type (4)
Industry depression has the advantages of industry depression
Educational Codeforces Round 115 (Rated for Div. 2)
C语言-入门-基础-语法-[标识符,关键字,分号,空格,注释,输入和输出](三)
随机推荐
Global and Chinese market of bipolar generators 2022-2028: Research Report on technology, participants, trends, market size and share
什么是uid?什么是Auth?什么是验证器?
Awk from getting started to digging in (4) user defined variables
《网络是怎么样连接的》读书笔记 - 集线器、路由器和路由器(三)
You can see the employment prospects of PMP project management
随机事件的关系与运算
1211 or chicken and rabbit in the same cage
Global and Chinese markets of hemoglobin analyzers in care points 2022-2028: Research Report on technology, participants, trends, market size and share
"How to connect the Internet" reading notes - FTTH
After unplugging the network cable, does the original TCP connection still exist?
Investment analysis and future production and marketing demand forecast report of China's paper industry Ⓥ 2022 ~ 2028
Reload CUDA and cudnn (for tensorflow and pytorch) [personal sorting summary]
C语言-入门-基础-语法-[变量,常亮,作用域](五)
Awk from entry to earth (12) awk can also write scripts to replace the shell
At the age of 30, I changed to Hongmeng with a high salary because I did these three things
Global and Chinese markets of water heaters in Saudi Arabia 2022-2028: Research Report on technology, participants, trends, market size and share
How to ensure the uniqueness of ID in distributed environment
CLion-控制台输出中文乱码
C语言-入门-基础-语法-数据类型(四)
26. Delete duplicates in the ordered array (fast and slow pointer de duplication)