当前位置:网站首页>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 
边栏推荐
- Understanding of incomplete types
- Embedded dynamic Arabic string conversion LCD display string [thanks for Jianguo ambition]
- What is an interface? What is interface testing?
- LeetCode122. The best time to buy and sell stocks II
- Leetcode daily question - 710 Random numbers in the blacklist
- Is it safe to open a dig money account? Is it reliable?
- Anr problem - camera related debug
- AI deep dive of Huawei cloud
- Manual backup and restore of DHCP server
- PHP uses stack to solve maze problem
猜你喜欢

Bitbucket failed to pull the warehouse Using SSH

17 `bs object Node name h3 Parent ` parents get parent node ancestor node

pyechart绘制多条y轴折线图

Alist+raidrive gives the computer a complete 8billion GB hard disk drive

AI deep dive of Huawei cloud

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

16 `bs object Node name Div. attribute contents ` children descendants get child nodes and descendants

What is an interface? What is interface testing?

Pyechart drawing multiple Y-axis line graphs

17 `bs对象.节点名h3.parent` parents 获取父节点 祖先节点
随机推荐
LeetCode560. Subarray with and K
[Note: circuit intégré MOS analogique] référence de bande Gap (principe de base + mode courant + circuit en mode tension)
Leetcode daily question - 515 Find the maximum value in each tree row
Can you make money by speculating in stocks? It's safe to open an account
Openfire 3.8.2 cluster configuration
LeetCode121. The best time to buy and sell stocks
Is the rapid SSL wildcard certificate genuine in 1981
Alibaba cloud MSE full link grayscale solution practice based on Apache apisik
External parameter calibration method for 16 line mechanical radar and monocular camera based on solid state lidar
直播预告|SQL也能玩转工业级机器学习?MLOps meetup V3带你一探究竟!
Application of the purple book p113map of ananagrams (uva156)
Application practice | 1billion data second level correlation. Huolala's OLAP System Evolution Based on Apache Doris (with PPT download)
城市大脑知识图谱构建及应用研究
零基础自学SQL课程 | SQL中的日期函数大全
Ehcache configuration data, convenient for self checking
Pie (poj3122) super detailed and easy to understand binary introduction
LeetCode986. 区间列表的交集
LeetCode213. House raiding II
Interface test process
LeetCode117. Populate the next right node pointer for each node_ II