当前位置:网站首页>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
边栏推荐
- Lua determines whether the current time is the time of the day
- Research on the value of background repeat of background tiling
- Three dimensional dice realize 3D cool rotation effect (with complete source code) (with animation code)
- Data is stored in the form of table
- AutoCAD - command repetition, undo and redo
- 【论文笔记】Multi-Goal Reinforcement Learning: Challenging Robotics Environments and Request for Research
- Generate filled text and pictures
- 十年不用一次的JVM调用
- LeetCode之单词搜索(回溯法求解)
- Use of snippets in vscode (code template)
猜你喜欢

Ue4/ue5 illusory engine, material part (III), material optimization at different distances

django连接数据库报错,这是什么原因

Pdf to DWG in CAD

AutoCAD - workspace settings

Redis has four methods for checking big keys, which are necessary for optimization

On-off and on-off of quality system construction

C4D simple cloth (version above R21)

JVM call not used once in ten years

BUUCTF MISC

Unity3d learning notes
随机推荐
Judge the position of the monster in the role under unity3d
2022 / 7 / 1 Résumé de l'étude
Database under unity
Unity enables mobile phone vibration
Basic knowledge points of dictionary
MD5 bypass
AutoCAD - Zoom previous
54. 螺旋矩阵 & 59. 螺旋矩阵 II ●●
JVM call not used once in ten years
十年不用一次的JVM调用
This article is good
Unity check whether the two objects have obstacles by ray
用 Jmeter 工具做个小型压力测试
Redis has four methods for checking big keys, which are necessary for optimization
The first topic of ape Anthropology
Cocos create Jiugongge pictures
Three dimensional dice realize 3D cool rotation effect (with complete source code) (with animation code)
The difference between heap and stack
PR first time
3dsmax snaps to frozen objects