当前位置:网站首页>C语言快速解决反转链表
C语言快速解决反转链表
2022-07-04 22:35:00 【我不是萧海哇~~~~】
反转链表
题目描述:
给定一个单链表的头结点pHead(该头节点是有值的,比如在下图,它的val是1),长度为n,反转该链表后,返回新链表的表头。
数据范围: 0≤n≤1000
要求:空间复杂度 O(1) ,时间复杂度 O(n)O(n)
如当输入链表{1,2,3}时,经反转后,原链表变为{3,2,1},所以对应的输出为{3,2,1}。
以上转换过程如下图所示:

本文介绍两种方法:
(1)就地反转
(2)双链表法
(感觉两种方法差不多…)
/*struct ListNode { int val; struct ListNode *next; }; */
//用c语言实现
struct ListNode* ReverseList(struct ListNode* pHead ) {
struct ListNode *pre=NULL;//pre指针指向已经反转好的链表的最后一个节点,最开始没有反转,所以指向null
struct ListNode *cur=pHead;//cur指针指向待反转链表的第一个节点,最开始第一个节点待反转,所以指向head
struct ListNode *nex=NULL;//用来保存断链结点,即指针指向待反转链表的第二个节点
while(cur){
nex=cur->next;
cur->next=pre;//实现断链操作
pre=cur;//反转的节点
cur=nex;//指向之前断链的节点
}
return pre;
}
//双链表实现
struct ListNode* ReverseList(struct ListNode* pHead ) {
struct ListNode *newHead=NULL;
while(pHead!=NULL){
struct ListNode *temp=pHead->next; //保存断链结点
pHead->next=newHead;//将新链表挂到访问的原链表节点之后
newHead=pHead;
pHead=temp;
}
return newHead;
}
注:还能使用栈来实现这个题,先进后出输出刚好实现反转
原文:https://blog.csdn.net/qq_46027119/article/details/124182305
边栏推荐
- Photoshop批量给不同的图片添加不同的编号
- Attack and defense world misc advanced grace-50
- Persistence mechanism of redis
- 记录:关于Win10系统中Microsoft Edge上的网页如何滚动截屏?
- Advanced area of attack and defense world misc 3-11
- Gnawing down the big bone - sorting (II)
- Common methods in string class
- Mongodb aggregation operation summary
- Feature scaling normalization
- Redis入门完整教程:API的理解和使用
猜你喜欢

Unity Xiuxian mobile game | Lua dynamic sliding function (specific implementation of three source codes)

Redis getting started complete tutorial: hash description

Sobel filter

Naacl-22 | introduce the setting of migration learning on the prompt based text generation task

新版判断PC和手机端代码,手机端跳转手机端,PC跳转PC端最新有效代码

Redis入门完整教程:事务与Lua

Attack and defense world misc advanced area Hong

A complete tutorial for getting started with redis: redis shell
![[machine learning] handwritten digit recognition](/img/26/cabdc5c92035181d82f6f809e6df0f.png)
[machine learning] handwritten digit recognition

Advanced area of attack and defense world misc 3-11
随机推荐
Redis入门完整教程:Redis Shell
Redis入门完整教程:Bitmaps
微信公众号解决从自定义菜单进入的缓存问题
Attack and defense world misc master advanced zone 001 normal_ png
Redis入门完整教程:有序集合详解
[Lua] Int64 support
The difference between Max and greatest in SQL
NFT insider 64: e-commerce giant eBay submitted an NFT related trademark application, and KPMG will invest $30million in Web3 and metauniverse
How to send a reliable request before closing the page
环境加密技术解析
Redis introduction complete tutorial: detailed explanation of ordered collection
Sword finger offer 68 - I. nearest common ancestor of binary search tree
Photoshop batch adds different numbers to different pictures
Redis入门完整教程:HyperLogLog
Redis入门完整教程:客户端通信协议
Attack and Defense World MISC Advanced Area Erik baleog and Olaf
Set up a website with a sense of ceremony, and post it to 1/2 of the public network through the intranet
Duplicate ADMAS part name
Three stage operations in the attack and defense drill of the blue team
Redis入门完整教程:慢查询分析