当前位置:网站首页>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
}
};边栏推荐
- What is permission? What is a role? What are users?
- Reading notes of how the network is connected - understanding the basic concepts of the network (I)
- If you can quickly generate a dictionary from two lists
- Awk from getting started to digging in (9) circular statement
- "How to connect the network" reading notes - Web server request and response (4)
- Educational Codeforces Round 115 (Rated for Div. 2)
- Service call feign of "micro service"
- "How to connect the Internet" reading notes - FTTH
- AMLOGIC gsensor debugging
- There are 100 people eating 100 apples, one adult eating 4 apples, and four children eating 1 apple. How can they eat exactly 100 apples? Use any high-level language you are familiar with
猜你喜欢

2022-2028 global elastic strain sensor industry research and trend analysis report

Codeforces Round #793 (Div. 2)(A-D)

2022-2028 global industry research and trend analysis report on anterior segment and fundus OTC detectors

Ehrlich sieve + Euler sieve + interval sieve
![C language - Introduction - Foundation - syntax - [operators, type conversion] (6)](/img/3f/4d8f4c77d9fde5dd3f53ef890ecfa8.png)
C language - Introduction - Foundation - syntax - [operators, type conversion] (6)
![C language - Introduction - Foundation - syntax - [identifier, keyword, semicolon, space, comment, input and output] (III)](/img/89/0f5f4ba07c637b09abe873016cba2d.png)
C language - Introduction - Foundation - syntax - [identifier, keyword, semicolon, space, comment, input and output] (III)

Sequence model

You can see the employment prospects of PMP project management
](/img/89/0f5f4ba07c637b09abe873016cba2d.png)
C语言-入门-基础-语法-[标识符,关键字,分号,空格,注释,输入和输出](三)

Target detection -- intensive reading of yolov3 paper
随机推荐
Awk from entry to earth (18) GAW K line manual
Reading notes on how to connect the network - hubs, routers and routers (III)
Report on research and investment prospect prediction of China's electronic grade sulfuric acid industry (2022 Edition)
At the age of 30, I changed to Hongmeng with a high salary because I did these three things
Awk from entry to earth (15) awk executes external commands
Basic discipline formula and unit conversion
《网络是怎么样连接的》读书笔记 - 认识网络基础概念(一)
Analysis report on the production and marketing demand and investment forecast of tellurium dioxide in the world and China Ⓣ 2022 ~ 2027
20220701 Barbalat引理证明
What is permission? What is a role? What are users?
什么是权限?什么是角色?什么是用户?
In depth investigation and Strategic Research Report on China's motion controller Market (2022 Edition)
Launpad | basic knowledge
Global and Chinese markets for laser assisted liposuction (LAL) devices 2022-2028: Research Report on technology, participants, trends, market size and share
Jianzhi offer 09 realizes queue with two stacks
The map set type is stored in the form of key value pairs, and the iterative traversal is faster than the list set
Use Alibaba cloud NPM image acceleration
Research Report on the development trend and Prospect of global and Chinese zinc antimonide market Ⓚ 2022 ~ 2027
DR6018-CP01-wifi6-Qualcomm-IPQ6010-IPQ6018-FAMILY-2T2R-2.5G-ETH-port-CP01-802-11AX-MU-MIMO-OFDMA
2022-2028 global industrial gasket plate heat exchanger industry research and trend analysis report