当前位置:网站首页>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 
边栏推荐
- LeetCode188. 买卖股票的最佳时机IV
- Microsoft's exclusive payment function has also been perfectly unlocked
- LeetCode117. 填充每个节点的下一个右侧节点指针_II
- 【激活函数】
- Activate function
- The comprehensive application of the setstack computer (uva12096) Purple Book p116stl
- [software test] 2022 national unified college enrollment examination
- Leetcode daily question - 515 Find the maximum value in each tree row
- 关于不完全类型的认识
- CVPR 2022|极具创意&美感的文字生成方法!支持任意输入
猜你喜欢

Bitbucket 使用 SSH 拉取仓库失败的问题

CVPR 2022|极具创意&美感的文字生成方法!支持任意输入

pyechart绘制多条y轴折线图

16 `bs对象.节点名div.属性contents` children descendants 获取子节点 子孙节点
![[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)

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

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

Interface test process

Lumiprobe proteorange protein gel dye instructions

【激活函数】
随机推荐
河狸生存记:90后女博士与AI开发者们
PHP uses stack to solve maze problem
Openfire user and group relationship migration
[Note: analog MOS integrated circuit] bandgap reference (basic principle + current mode + voltage mode circuit explanation)
Ehcache configuration data, convenient for self checking
GlobalSign的泛域名SSL证书
To be a cross-border e-commerce, you must learn to use PRA software, free your hands and improve efficiency!
QStringLiteral(str)
Biovendor free light chain( κ and λ) Test steps of ELISA Kit
LeetCode56. 合并区间
Manual backup and restore of DHCP server
[software test] 2022 national unified college enrollment examination
接口用例设计
LeetCode188. 买卖股票的最佳时机IV
LeetCode117. 填充每个节点的下一个右侧节点指针_II
Leetcode daily question - 515 Find the maximum value in each tree row
AI deep dive of Huawei cloud
Is it safe to open an account on great wisdom
[book club issue 13] packaging format of video files
LeetCode560. Subarray with and K