当前位置:网站首页>Sword finger offer:[day 2 linked list (simple)] --- > reverse linked list
Sword finger offer:[day 2 linked list (simple)] --- > reverse linked list
2022-06-28 21:46:00 【Love you, little pig】
List of articles
One 、 Title Description
Define a function , Enter the head node of a linked list , Invert the linked list and output the head node of the inverted linked list .
Example
Input : 1->2->3->4->5->NULL
Output : 5->4->3->2->1->NULL
Limit
0 <= Number of nodes <= 5000
Two 、 Thought analysis
Ideas
① Suppose the linked list is 1 → 2 → 3 → ∅ 1 \rightarrow 2 \rightarrow 3 \rightarrow \varnothing 1→2→3→∅, We want to change it to ∅ ← 1 ← 2 ← 3 \varnothing \leftarrow 1 \leftarrow 2 \leftarrow 3 ∅←1←2←3.
② When traversing a linked list , Change the current node'snextThe pointer is changed to point to the previous node . Because it's a one-way list , Therefore, the previous node must be stored in advance . meanwhile , Of the current node next After the pointer is changed to point to the previous node , Continue to process the next node . The current node's next The pointer no longer points to the following node , We can't find the next node , Therefore, you are changing the current node next Before the pointer , You also need to store the following node . Last , After the current node is reversed , Move to the next node , The node in front of the current node also moves back .
③ Loop through the above steps , Until the current node is empty . At this point, it means that we have reached the end of the chain next node , Just return the previous node of the current node .
3、 ... and 、 The overall code
The overall code is as follows
struct ListNode* reverseList(struct ListNode* head){
// establish pre The pointer , Point to the node before the current node . This is because if you want to reverse the current node , Of the current node next Point to pre
struct ListNode* pre = NULL;
// The current node is not empty , Description can also be reversed
while(head){
// Save the next node of the current node , Because the current node is reversed , Of the current node next Point to pre 了 , You don't know the next node that needs to be reversed
struct ListNode* next = head->next;
head->next = pre; // Reverse
pre = head; //pre move backward
head = next; // Move the current node backward
}
// because head Finally, it will be the node behind the last node , It's an empty , So back pre
return pre;
}
function , The test passed 
边栏推荐
- 运动App如何实现端侧后台保活,让运动记录更完整?
- LeetCode:二叉树展开为链表_114
- How to make up the PMP Exam? How much is the make-up exam?
- 河狸生存记:90后女博士与AI开发者们
- 接口测试流程
- Leetcode56. consolidation interval
- Leetcode daily question - 515 Find the maximum value in each tree row
- Is it safe to open a dig money account? Is it reliable?
- Bitbucket 使用 SSH 拉取仓库失败的问题
- Is the rapid SSL wildcard certificate genuine in 1981
猜你喜欢

【笔记:模拟MOS集成电路】带隙基准(基本原理+电流模+电压模电路详解)

Study on bifunctional crosslinker lumiprobe sulfoacyanine 7 dicarboxylic acid

安全 创新 实践|海泰方圆受邀参加“数字时代的网信创新与价值共创”技术交流研讨会

Interface use case design

Study on luminiprobe non fluorescent azide -- 3-azido propanol

视觉弱监督学习研究进展

Pie (poj3122) super detailed and easy to understand binary introduction

MySQL system error occurred 1067

PHP uses stack to solve maze problem

Security dilemma of NFT liquidity agreement - Analysis of the hacked event of NFT loan agreement xcarnival
随机推荐
Embedded dynamic Arabic string conversion LCD display string [thanks for Jianguo ambition]
LeetCode:合并K个升序链表_23
构建实战化防御体系之立体防渗透
Lua源码剖析:一. lua变量类型可变特性在C代码中实现。
Security dilemma of NFT liquidity agreement - Analysis of the hacked event of NFT loan agreement xcarnival
LeetCode877. 石子游戏
Microsoft's exclusive payment function has also been perfectly unlocked
Lua source code analysis: 1 Lua variable type mutability is implemented in C code.
LeetCode986. 区间列表的交集
The comprehensive application of the setstack computer (uva12096) Purple Book p116stl
Definition and precautions of genuine St link/v2 j-link jtag/swd pin
LeetCode122. 买卖股票的最佳时机II
LeetCode226. 翻转二叉树
接口测试流程
Is it safe to open a stock trading account? Is it reliable?
Openfire 3.8.2 cluster configuration
The further application of Li Kou tree
How to add logs to debug anr problems
LeetCode226. Flip binary tree
二叉树类题目 力扣