当前位置:网站首页>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 
边栏推荐
- 什么是接口?什么是接口测试?
- 17 `bs object Node name h3 Parent ` parents get parent node ancestor node
- QT 一个控件的坐标怎么相对固定显示在另一个控件上(坐标系)
- Recommend two high-quality Wallpaper software
- 运动App如何实现端侧后台保活,让运动记录更完整?
- 【筆記:模擬MOS集成電路】帶隙基准(基本原理+電流模+電壓模電路詳解)
- [software test] 2022 national unified college enrollment examination
- How to make up the PMP Exam? How much is the make-up exam?
- Lumiprobe non fluorescent alkyne research - dbco NHS ester
- Lua源码剖析:一. lua变量类型可变特性在C代码中实现。
猜你喜欢

CVPR 2022|极具创意&美感的文字生成方法!支持任意输入
![[Note: circuit intégré MOS analogique] référence de bande Gap (principe de base + mode courant + circuit en mode tension)](/img/cd/be62272d465ca990456c222b38df67.png)
[Note: circuit intégré MOS analogique] référence de bande Gap (principe de base + mode courant + circuit en mode tension)

Biovendor free light chain( κ and λ) Test steps of ELISA Kit

The rogue downloader named by 315 is back

Lumiprobe proteorange protein gel dye instructions

视觉弱监督学习研究进展

How to analyze the relationship between enterprise digital transformation and data asset management?

什么是接口?什么是接口测试?

Interface test process
![[software test] 2022 national unified college enrollment examination](/img/9a/d76d7eb30a097d364fef28c2230e1a.png)
[software test] 2022 national unified college enrollment examination
随机推荐
[software test] 2022 national unified college enrollment examination
Leetcode56. consolidation interval
CORBA Architecture Guide (Common Object Request Broker Architecture)
Binary tree problems
Workplace tips | understanding the advantages of the position "knowing people"
Mongodb - replica set and sharding
How do independent site sellers efficiently manage complex Facebook pages?
职场小技巧 | 了解岗位优势三板斧之“识人”
接口测试流程
Application of Andy s first dictionary (uva10815) Purple Book p112set
LeetCode877. Stone game
Which is the most reliable and safe for a securities company to open an account
LeetCode117. Populate the next right node pointer for each node_ II
视觉弱监督学习研究进展
LeetCode226. Flip binary tree
LeetCode121. 买卖股票的最佳时机
Alibaba cloud MSE full link grayscale solution practice based on Apache apisik
The blocks problem (uva101) Purple Book p110vector application
力扣树的进一步应用
LeetCode213. 打家劫舍II