当前位置:网站首页>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
边栏推荐
- [Jianzhi offer] 6-10 questions
- UML图记忆技巧
- 攻防世界 MISC 進階區 Erik-Baleog-and-Olaf
- Taobao commodity review API interface (item_review get Taobao commodity review API interface), tmall commodity review API interface
- Redis démarrer le tutoriel complet: Pipeline
- The new version judges the code of PC and mobile terminal, the mobile terminal jumps to the mobile terminal, and the PC jumps to the latest valid code of PC terminal
- Unity Xiuxian mobile game | Lua dynamic sliding function (specific implementation of three source codes)
- 【ODX Studio編輯PDX】-0.2-如何對比Compare兩個PDX/ODX文件
- EditPlus--用法--快捷键/配置/背景色/字体大小
- MYSQL架构——逻辑架构
猜你喜欢

Redis入门完整教程:客户端通信协议

共创软硬件协同生态:Graphcore IPU与百度飞桨的“联合提交”亮相MLPerf

Unity vscode emmylua configuration error resolution

位运算符讲解

攻防世界 MISC 进阶 glance-50

Redis getting started complete tutorial: hash description

Redis introduction complete tutorial: Collection details

Redis introduction complete tutorial: detailed explanation of ordered collection

Hit the core in the advanced area of misc in the attack and defense world

Redis入门完整教程:Pipeline
随机推荐
How to send a reliable request before closing the page
Gnawing down the big bone - sorting (II)
Redis getting started complete tutorial: Key Management
MYSQL架构——逻辑架构
Redis的持久化机制
[try to hack] wide byte injection
Taobao commodity review API interface (item_review get Taobao commodity review API interface), tmall commodity review API interface
Naacl-22 | introduce the setting of migration learning on the prompt based text generation task
Redis入门完整教程:慢查询分析
Unity Xiuxian mobile game | Lua dynamic sliding function (specific implementation of three source codes)
Duplicate ADMAS part name
Erik baleog and Olaf, advanced area of misc in the attack and defense world
EditPlus--用法--快捷键/配置/背景色/字体大小
How to choose a securities company? Is it safe to open an account on your mobile phone
A complete tutorial for getting started with redis: hyperloglog
页面关闭前,如何发送一个可靠请求
图片懒加载的原理
The difference between Max and greatest in SQL
微信公众号解决从自定义菜单进入的缓存问题
【剑指offer】1-5题