当前位置:网站首页>C language to quickly solve the reverse linked list
C language to quickly solve the reverse linked list
2022-07-04 23:15:00 【I'm not Xiao Haiwa~~~~】
Reverse a linked list
Title Description :
Given the head node of a single linked list pHead( The header node has a value , For example, in the figure below , its val yes 1), The length is n, After reversing the linked list , Return the header of the new linked list .
Data range : 0≤n≤1000
requirement : Spatial complexity O(1) , Time complexity O(n)O(n)
For example, when entering a linked list {1,2,3} when , After reversal , The original linked list becomes {3,2,1}, So the corresponding output is {3,2,1}.
The above conversion process is shown in the figure below :

This paper introduces two methods :
(1) Reverse in place
(2) Double linked list method
( I think the two methods are similar …)
/*struct ListNode { int val; struct ListNode *next; }; */
// use c Language implementation
struct ListNode* ReverseList(struct ListNode* pHead ) {
struct ListNode *pre=NULL;//pre The pointer points to the last node of the inverted linked list , There was no reversal at first , So the point to null
struct ListNode *cur=pHead;//cur The pointer points to the first node of the linked list to be reversed , Wait for the first node to reverse , So the point to head
struct ListNode *nex=NULL;// Used to save broken chain nodes , That is, the pointer points to the second node of the linked list to be reversed
while(cur){
nex=cur->next;
cur->next=pre;// Realize the chain breaking operation
pre=cur;// Inverted node
cur=nex;// Point to the previously broken node
}
return pre;
}
// Double linked list implementation
struct ListNode* ReverseList(struct ListNode* pHead ) {
struct ListNode *newHead=NULL;
while(pHead!=NULL){
struct ListNode *temp=pHead->next; // Save the broken chain node
pHead->next=newHead;// Hang the new linked list after the visited original linked list node
newHead=pHead;
pHead=temp;
}
return newHead;
}
notes : You can also use stack to realize this problem , The first in and last out output just realizes the inversion
original text :https://blog.csdn.net/qq_46027119/article/details/124182305
边栏推荐
- Photoshop batch adds different numbers to different pictures
- Redis:Redis的事务
- Duplicate ADMAS part name
- Basic use and upgrade of Android native database
- PS style JS webpage graffiti board plug-in
- Redis introduction complete tutorial: slow query analysis
- 法国学者:最优传输理论下对抗攻击可解释性探讨
- 壁仞科技研究院前沿技术文章精选
- P2181 diagonal and p1030 [noip2001 popularization group] arrange in order
- ffmpeg快速剪辑
猜你喜欢

qt绘制网络拓补图(连接数据库,递归函数,无限绘制,可拖动节点)

JS card style countdown days

A complete tutorial for getting started with redis: Pipeline

Redis入门完整教程:初识Redis

vim编辑器知识总结

Editplus-- usage -- shortcut key / configuration / background color / font size

Redis introduction complete tutorial: slow query analysis

A mining of edu certificate station

Qt个人学习总结
![P2181 对角线和P1030 [NOIP2001 普及组] 求先序排列](/img/79/36c46421bce08284838f68f11cda29.png)
P2181 对角线和P1030 [NOIP2001 普及组] 求先序排列
随机推荐
LabVIEW中比较两个VI
法国学者:最优传输理论下对抗攻击可解释性探讨
Photoshop批量给不同的图片添加不同的编号
CTF competition problem solution STM32 reverse introduction
Actual combat simulation │ JWT login authentication
The caching feature of docker image and dockerfile
ScriptableObject
MariaDB's Galera cluster application scenario -- multi master and multi active databases
Redis introduction complete tutorial: client communication protocol
Photoshop batch adds different numbers to different pictures
MariaDB的Galera集群应用场景--数据库多主多活
Redis入门完整教程:哈希说明
Pagoda 7.9.2 pagoda control panel bypasses mobile phone binding authentication bypasses official authentication
MySQL数据库备份与恢复--mysqldump命令
MariaDB的Galera集群-双主双活安装设置
[ODX studio edit PDX] - 0.2-how to compare two pdx/odx files of compare
[graph theory] topological sorting
VIM editor knowledge summary
Redis introduction complete tutorial: detailed explanation of ordered collection
【二叉树】节点与其祖先之间的最大差值