当前位置:网站首页>Reverse one-way linked list of interview questions
Reverse one-way linked list of interview questions
2022-07-05 05:08:00 【ximen502_】
titled : Reverse a one-way linked list , Write algorithm steps or code .
Muddled . Today's learning is as follows ,
Article code reference https://blog.csdn.net/K346K346/article/details/93371829, thank !
#include<iostream>
using namespace std;
typedef struct linknode {
int value;
linknode * next;
} LinkNode;
/* Loop traversal realizes the inversion of one-way linked list */
LinkNode * Reverse(LinkNode* head) {
if (head == NULL || head->next == NULL) {
return head;
}
LinkNode* pre = head, *cur = head->next;
head->next = NULL;
while (cur != NULL) {
auto next = cur->next;
cur->next = pre;
pre = cur;
cur = next;
}
return pre;
}
void printLink(LinkNode* head) {
while (head != NULL) {
cout << head->value;
head = head->next;
if (head != NULL) {
cout << "->";
}
}
cout << endl;
}
/* Recursive implementation of one-way linked list inversion */
LinkNode* Reverse2(LinkNode* head) {
// Recursive termination condition
if (head == NULL || head->next == NULL) {
return head;
}
LinkNode* newHead = Reverse2(head->next);
head->next->next = head;
head->next = NULL;
return newHead;
}
int main(int argc, char* argv[]) {
LinkNode* head = NULL, *cur = NULL;
for (int i = 1; i <= 5; i++) {
LinkNode* node = new LinkNode;
node->value = i;
node->next = NULL;
if (head == NULL) {
head = node;
cur = head;
} else {
cur->next = node;
cur = node;
}
}
printLink(head);
// Reverse one-way list
auto newHead = Reverse(head);
printLink(newHead);
}
In fact, it can be summed up as
1. preservation cur Of next
2. modify cur Of next Point to pre
3. Move pre,cur The pointer :pre Point to cur,cur Point to previously saved next
loop 1,2,3 until cur by NULL end , Now pre It points to the inverted head, complete .
In fact, the data is still stored in the original place , It's just that the arrow points backwards .
About code compilation and execution , Feeling C++ Changed a lot , Structures can also be used new keyword , stay Linux above .
g++ -std=c++11 reverselink.cpp
./a.out
1->2->3->4->5
5->4->3->2->1
边栏推荐
- Detailed introduction of OSPF header message
- Use the command character to close the keyboard command of the notebook
- Page countdown
- Autocad-- dynamic zoom
- Use of snippets in vscode (code template)
- 被舆论盯上的蔚来,何时再次“起高楼”?
- 3dsmax2018 common operations and some shortcut keys of editable polygons
- 2022 / 7 / 1 Résumé de l'étude
- 2020-10-27
- AutoCAD - feature matching
猜你喜欢
3dsmax snaps to frozen objects
UE4/UE5 虚幻引擎,材质篇(三),不同距离的材质优化
AutoCAD - set layer
Unity ugui source code graphic
AutoCAD - feature matching
Research on the value of background repeat of background tiling
Use assimp library to read MTL file data
Use of snippets in vscode (code template)
Learning notes of "hands on learning in depth"
AutoCAD - scaling
随机推荐
Detailed explanation of the ranking of the best universities
UE4/UE5 虚幻引擎,材质篇(三),不同距离的材质优化
中国艾草行业研究与投资前景预测报告(2022版)
2022/7/1学习总结
Recherche de mots pour leetcode (solution rétrospective)
A complete attack chain
54. Spiral matrix & 59 Spiral matrix II ●●
Unity parallax infinite scrolling background
Unity find the coordinates of a point on the circle
Cocos create Jiugongge pictures
Ue4/ue5 illusory engine, material chapter, texture, compression and memory compression and memory
AutoCAD - Center zoom
win下一键生成当日的时间戳文件
AutoCAD - continuous annotation
2022/7/1學習總結
嵌入式数据库开发编程(零)
AutoCAD - set layer
LeetCode之单词搜索(回溯法求解)
Lua GBK and UTF8 turn to each other
mysql审计日志归档