当前位置:网站首页>Sword finger offer|: linked list (simple)
Sword finger offer|: linked list (simple)
2022-06-28 07:23:00 【_ Soren】
List of articles
List of articles
The finger of the sword Offer 06. Print linked list from end to end
Original link : Print linked list from end to end
Enter the head node of a linked list , Return the value of each node from the end to the end ( Return with array ).
Answer key :
Use the virtual head node to accept the inverted linked list , Then iterate through the list , Tail into return array .
class Solution {
public:
vector<int> reversePrint(ListNode* head) {
ListNode* dummy = new ListNode();
dummy = ReverseList(head);
vector<int> ans;
while (dummy != nullptr) {
ans.emplace_back(dummy->val);
dummy = dummy->next;
}
return ans;
}
// Reverse of linked list
ListNode* ReverseList(ListNode* head) {
ListNode* prev = nullptr;
ListNode* curr = head;
while (curr != nullptr) {
ListNode* next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
return prev;
}
};
The finger of the sword Offer 35. Replication of complex linked list
Original link : Replication of complex linked list
Please implement copyRandomList function , Copy a complex list . In complex linked list , Each node has one next The pointer points to the next node , One more random The pointer points to any node in the list or null.

Answer key :
We first split each node in the linked list into two connected nodes , For example, for a linked list A→B→C, We can split it into A→A′→B→B′→C→C’. For any original node S, Its node copies S′ That is, its successor node .
such , We can find each copy node directly S′ The random pointer to the node that should be pointed to , Is its original node S The random pointer to the node T Successor node T′. Note that the random pointer of the original node may be null , We need to judge this situation in particular .
When we complete the assignment of the random pointer of the copy node , We only need to split the linked list according to the types of the original node and the copy node , You only need to traverse it once . Also note that the successor node of the last copy node is empty , We need to judge this situation in particular .



class Solution {
public:
Node* copyRandomList(Node* head) {
if (head == nullptr) {
return nullptr;
}
for (Node* node = head; node != nullptr; node = node->next->next) {
Node* nodeNew = new Node(node->val);
nodeNew->next = node->next;
node->next = nodeNew;
}
for (Node* node = head; node != nullptr; node = node->next->next) {
Node* nodeNew = node->next;
nodeNew->random = (node->random != nullptr) ? node->random->next : nullptr;
}
Node* headNew = head->next;
for (Node* node = head; node != nullptr; node = node->next) {
Node* nodeNew = node->next;
node->next = node->next->next;
nodeNew->next = (nodeNew->next != nullptr) ? nodeNew->next->next : nullptr;
}
return headNew;
}
};
边栏推荐
- 8 张图 | 剖析 Eureka 的首次同步注册表
- Wechat applets - basics takes you to understand the life cycle of applets (I)
- SQL statement optimization steps (1)
- 实时数据库 - 笔记
- R 语言 ggmap 可视化集群
- goland IDE和delve调试位于kubernetes集群中的go程序
- [C#][转载]furion框架地址和教程地址
- 未来互联网人才还稀缺吗?哪些技术方向热门?
- Mysql8.0和Mysql5.0访问jdbc连接
- Tencent continued to lay off staff in the second half of the year, and all business groups reduced by at least 10%. What do you think of this? Followers
猜你喜欢

一个小工具可以更快的写爬虫

linux下修改mysql用户名root

My MVVM open source project "travel epidemic prevention app" has been released

在idea中,get和set方法爆红可能是没有安装Lombok插件

BACnet/IP網關如何采集樓宇集中控制系統數據

Mysql57 zip file installation

What is a consistent hash? What scenarios can it be applied to?

Voice network VQA: make the user's subjective experience of unknown video quality in real-time interaction known

Techo day Tencent technology open day, June 28 online waiting for you!

Open62541 import nodeset file directly
随机推荐
【C语言】详解 C 语言获取数组长度
C language tutorial
阿里云服务器创建快照、回滚磁盘
A small code editor can also run programs -- a summary of sublime Text3 running programs in various languages
MMR rearrangement (similarity is calculated by editing distance and repeatability)
Principle and practice of bytecode reference detection
Recommend several 0 code, free, learning and using visualization tools
R language hitters data analysis
XML序列化向后兼容
Pytorch RNN learning notes
R and RGL draw 3D knots
金山云团队分享 | 5000字读懂Presto如何与Alluxio搭配
File header information cross reference table
"Jay bear" plummeted by 96.6%. Why is NFT with star goods cold?
R language ggmap
[c #] [reprint]furion frame address and tutorial address
Evolution of vivo push platform architecture
网传互联网公司加班表,排名第一的没悬念
Jinshan cloud team shared | 5000 words to understand how Presto matches with alluxio
Server body 18: understanding and thinking of UDP reliable transmission (feeling from reading Yunfeng blog)