当前位置:网站首页>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
}
};边栏推荐
- Global and Chinese markets of hemoglobin analyzers in care points 2022-2028: Research Report on technology, participants, trends, market size and share
- Awk from getting started to digging in (9) circular statement
- Awk from entry to penetration (6) regular matching
- 2022-2028 global visual quality analyzer industry research and trend analysis report
- 老掉牙的 synchronized 锁优化,一次给你讲清楚!
- What is inner connection and outer connection? What are the uses and benefits
- If you can quickly generate a dictionary from two lists
- Global and Chinese markets of thrombography hemostasis analyzer (TEG) 2022-2028: Research Report on technology, participants, trends, market size and share
- [C Advanced] file operation (2)
- Launpad | 基础知识
猜你喜欢

Jianzhi offer 09 realizes queue with two stacks

Explain TCP protocol in detail three handshakes and four waves

Opencv environment construction (I)

CLion-控制台输出中文乱码
![C language - Introduction - Foundation - syntax - [variable, constant light, scope] (V)](/img/dc/5c8077c10cdc7ad6e6f92dedfbe797.png)
C language - Introduction - Foundation - syntax - [variable, constant light, scope] (V)

Some points needing attention in PMP learning

At the age of 30, I changed to Hongmeng with a high salary because I did these three things

2022-2028 global seeder industry research and trend analysis report

26. Delete duplicates in the ordered array (fast and slow pointer de duplication)

2022-2028 global probiotics industry research and trend analysis report
随机推荐
2022-2028 global visual quality analyzer industry research and trend analysis report
Sequence model
Industry depression has the advantages of industry depression
Review of last week's hot spots (6.27-7.3)
HMS core helps baby bus show high-quality children's digital content to global developers
Langage C - démarrer - base - syntaxe - [opérateur, conversion de type] (vi)
20220701 Barbalat引理证明
Report on investment analysis and prospect trend prediction of China's MOCVD industry Ⓤ 2022 ~ 2028
Codeforces Round #793 (Div. 2)(A-D)
2022-2028 global elastic strain sensor industry research and trend analysis report
Global and Chinese markets for laser assisted liposuction (LAL) devices 2022-2028: Research Report on technology, participants, trends, market size and share
DR6018-CP01-wifi6-Qualcomm-IPQ6010-IPQ6018-FAMILY-2T2R-2.5G-ETH-port-CP01-802-11AX-MU-MIMO-OFDMA
LeetCode 74. Search 2D matrix
What is inner connection and outer connection? What are the uses and benefits
保姆级JDEC增删改查练习
Global and Chinese market of sampler 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese market of wheel hubs 2022-2028: Research Report on technology, participants, trends, market size and share
[error record] no matching function for call to 'cacheflush' cacheflush();)
Flutter 小技巧之 ListView 和 PageView 的各种花式嵌套
Awk from entry to earth (14) awk output redirection