当前位置:网站首页>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 
边栏推荐
- Lua源码剖析:一. lua变量类型可变特性在C代码中实现。
- Interface test process
- Leetcode daily question - 515 Find the maximum value in each tree row
- LeetCode213. 打家劫舍II
- Bitbucket 使用 SSH 拉取仓库失败的问题
- Bitbucket failed to pull the warehouse Using SSH
- Comprehensive evaluation of easy-to-use and powerful PDF reading software: PDF expert, marginnote, liquidtext, notability, goodnotes, Zotero
- Leetcode56. consolidation interval
- AI deep dive of Huawei cloud
- LeetCode121. 买卖股票的最佳时机
猜你喜欢

Figure neural network can also be used as CV backbone model. Huawei Noah Vig architecture is comparable to CNN and transformer

Microsoft's exclusive payment function has also been perfectly unlocked

Zero foundation self-study SQL course | complete collection of date functions in SQL

直播预告|SQL也能玩转工业级机器学习?MLOps meetup V3带你一探究竟!

Bitbucket failed to pull the warehouse Using SSH

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

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

API gateway Apache APIs IX helps the evolution of snowball dual active architecture

User network model and QoE

I almost ran away
随机推荐
Alist+raidrive gives the computer a complete 8billion GB hard disk drive
CORBA Architecture Guide (Common Object Request Broker Architecture)
The rogue downloader named by 315 is back
Understanding web automated testing
LeetCode121. 买卖股票的最佳时机
Is it safe to open a dig money account? Is it reliable?
LeetCode:二叉树展开为链表_114
Lua源码剖析:一. lua变量类型可变特性在C代码中实现。
Golang JSON serializing and deserializing strings deserializing to map[string]interface{}
视觉弱监督学习研究进展
Bitbucket 使用 SSH 拉取仓库失败的问题
Web自动化工具选择
API gateway Apache APIs IX helps the evolution of snowball dual active architecture
After easycvr creates a new user, the video access page cannot be clicked. Fix the problem
Lumiprobe proteorange protein gel dye instructions
Smarca2 antibody study: abnova smarca2 monoclonal antibody protocol
Security dilemma of NFT liquidity agreement - Analysis of the hacked event of NFT loan agreement xcarnival
Lumiprobe lumizol RNA extraction reagent solution
Workplace tips | understanding the advantages of the position "knowing people"
Leetcode daily question - 30 Concatenate substrings of all words