当前位置:网站首页>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
边栏推荐
- UML diagram memory skills
- The overview and definition of clusters can be seen at a glance
- One of the commonly used technical indicators, reading boll Bollinger line indicators
- Redis: redis configuration file related configuration and redis persistence
- Serial port data frame
- The solution to the lack of pcntl extension under MAMP, fatal error: call to undefined function pcntl_ signal()
- Hit the core in the advanced area of misc in the attack and defense world
- Breakpoint debugging under vs2019 c release
- Redis getting started complete tutorial: publish and subscribe
- MySQL Architecture - user rights and management
猜你喜欢
Lost in the lock world of MySQL
攻防世界 MISC 进阶区 3-11
Persistence mechanism of redis
Redis入门完整教程:初识Redis
小程序vant tab组件解决文字过多显示不全的问题
Set up a website with a sense of ceremony, and post it to 1/2 of the public network through the intranet
攻防世界 MISC 高手进阶区 001 normal_png
Attack and defense world misc advanced grace-50
【室友用一局王者荣耀的时间学会了用BI报表数据处理】
Unity-VScode-Emmylua配置报错解决
随机推荐
A complete tutorial for getting started with redis: Pipeline
How to choose a securities company? Is it safe to open an account on your mobile phone
攻防世界 MISC 进阶区 can_has_stdio?
Redis getting started complete tutorial: publish and subscribe
Google Earth engine (GEE) - tasks upgrade enables run all to download all images in task types with one click
Redis入门完整教程:有序集合详解
Redis的持久化机制
Unity vscode emmylua configuration error resolution
9 - class
Redis入门完整教程:初识Redis
模拟摇杆控制舵机
Unity Xiuxian mobile game | Lua dynamic sliding function (specific implementation of three source codes)
Editplus-- usage -- shortcut key / configuration / background color / font size
Redis introduction complete tutorial: Collection details
Attack and defense world misc advanced zone 2017_ Dating_ in_ Singapore
通过Go语言创建CA与签发证书
Redis introduction complete tutorial: client communication protocol
记录:关于Win10系统中Microsoft Edge上的网页如何滚动截屏?
Redis入門完整教程:Pipeline
常用技术指标之一文读懂BOLL布林线指标