当前位置:网站首页>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
边栏推荐
- django连接数据库报错,这是什么原因
- Data is stored in the form of table
- Simple HelloWorld color change
- Forecast report on research and investment prospects of Chinese wormwood industry (2022 Edition)
- AutoCAD - window zoom
- 《动手学深度学习》学习笔记
- Research and investment forecast report of adamantane industry in China (2022 Edition)
- 被舆论盯上的蔚来,何时再次“起高楼”?
- The first topic of ape Anthropology
- 669. 修剪二叉搜索树 ●●
猜你喜欢
Generate filled text and pictures
Rip notes [rip message security authentication, increase of rip interface measurement]
Ue4/ue5 illusory engine, material chapter, texture, compression and memory compression and memory
BUUCTF MISC
Do a small pressure test with JMeter tool
54. Spiral matrix & 59 Spiral matrix II ●●
JVM call not used once in ten years
Applet live + e-commerce, if you want to be a new retail e-commerce, use it!
Page countdown
小程序直播+电商,想做新零售电商就用它吧!
随机推荐
BUUCTF MISC
Autocad-- dynamic zoom
Judge the position of the monster in the role under unity3d
The next key of win generates the timestamp file of the current day
An article takes you to thoroughly understand descriptors
54. 螺旋矩阵 & 59. 螺旋矩阵 II ●●
AutoCAD - continuous annotation
LeetCode之單詞搜索(回溯法求解)
Autocad-- Real Time zoom
775 Div.1 B. integral array mathematics
3dsmax2018 common operations and some shortcut keys of editable polygons
使用命令符关闭笔记本自带键盘命令
Unity shot tracking object
AutoCAD - command repetition, undo and redo
64 horses, 8 tracks, how many times does it take to find the fastest 4 horses at least
Time format conversion
《动手学深度学习》学习笔记
Common technologies of unity
【Leetcode】1352. 最后 K 个数的乘积
中国针状焦行业发展研究与投资价值报告(2022版)