当前位置:网站首页>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
}
};边栏推荐
- Flutter 小技巧之 ListView 和 PageView 的各種花式嵌套
- DR6018-CP01-wifi6-Qualcomm-IPQ6010-IPQ6018-FAMILY-2T2R-2.5G-ETH-port-CP01-802-11AX-MU-MIMO-OFDMA
- You can see the employment prospects of PMP project management
- 【LeetCode 42】501. Mode in binary search tree
- Leetcode topic [array] - 121 - the best time to buy and sell stocks
- Simulate EF dbcontext with MOQ - mocking EF dbcontext with MOQ
- 2022-2028 global seeder industry research and trend analysis report
- C language - Introduction - Foundation - syntax - [main function, header file] (II)
- Mac platform forgets the root password of MySQL
- Research and investment strategy report of China's electronic hydrogen peroxide industry (2022 Edition)
猜你喜欢

Target detection -- intensive reading of yolov3 paper

Implementation principle of redis string and sorted set

Some points needing attention in PMP learning

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

Educational Codeforces Round 115 (Rated for Div. 2)

Dede plug-in (multi-function integration)

Ehrlich sieve + Euler sieve + interval sieve

2022-2028 global probiotics industry research and trend analysis report

ArrayBuffer

到底什么才是DaaS数据即服务?别再被其他DaaS概念给误导了
随机推荐
20220701 Barbalat引理证明
《网络是怎么样连接的》读书笔记 - WEB服务端请求和响应(四)
Flutter tips: various fancy nesting of listview and pageview
The map set type is stored in the form of key value pairs, and the iterative traversal is faster than the list set
ArrayBuffer
Research Report on the development trend and Prospect of global and Chinese zinc antimonide market Ⓚ 2022 ~ 2027
Research and investment strategy report of China's electronic hydrogen peroxide industry (2022 Edition)
Research Report on research and investment prospects of China's testing machine industry (2022 Edition)
How do microservices aggregate API documents? This wave of show~
Some points needing attention in PMP learning
Analysis report on the development status and investment planning of China's modular power supply industry Ⓠ 2022 ~ 2028
Investment analysis and future production and marketing demand forecast report of China's paper industry Ⓥ 2022 ~ 2028
Opencv environment construction (I)
Mantis creates users without password options
Awk from entry to earth (18) GAW K line manual
Report on research and investment prospect prediction of China's electronic grade sulfuric acid industry (2022 Edition)
2022-2028 global small batch batch batch furnace industry research and trend analysis report
2022-2028 global tensile strain sensor industry research and trend analysis report
Les différents modèles imbriqués de listview et Pageview avec les conseils de flutter
《网络是怎么样连接的》读书笔记 - FTTH