当前位置:网站首页>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
边栏推荐
- 位运算符讲解
- The sandbox has reached a cooperation with digital Hollywood to accelerate the economic development of creators through human resource development
- 【机器学习】手写数字识别
- 【剑指Offer】6-10题
- Co create a collaborative ecosystem of software and hardware: the "Joint submission" of graphcore IPU and Baidu PaddlePaddle appeared in mlperf
- 攻防世界 MISC 进阶区 can_has_stdio?
- Wechat official account solves the cache problem of entering from the customized menu
- Redis: redis configuration file related configuration and redis persistence
- 小程序vant tab组件解决文字过多显示不全的问题
- 图片懒加载的原理
猜你喜欢
Redis入门完整教程:初识Redis
Explanation of bitwise operators
Redis入門完整教程:Pipeline
Redis introduction complete tutorial: detailed explanation of ordered collection
小程序vant tab组件解决文字过多显示不全的问题
Redis introduction complete tutorial: slow query analysis
A complete tutorial for getting started with redis: getting to know redis for the first time
[Jianzhi offer] 6-10 questions
SHP data making 3dfiles white film
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
随机推荐
Summary of wechat applet display style knowledge points
Wake up day, how do I step by step towards the road of software testing
【剑指offer】1-5题
集群的概述与定义,一看就会
One of the commonly used technical indicators, reading boll Bollinger line indicators
Redis introduction complete tutorial: slow query analysis
Async await used in map
Redis入门完整教程:Redis使用场景
MySQL Architecture - user rights and management
【ODX Studio編輯PDX】-0.2-如何對比Compare兩個PDX/ODX文件
攻防世界 MISC 进阶区 hit-the-core
记录:关于Win10系统中Microsoft Edge上的网页如何滚动截屏?
A complete tutorial for getting started with redis: getting to know redis for the first time
Unity vscode emmylua configuration error resolution
Notepad++--编辑的技巧
Redis introduction complete tutorial: Collection details
[Jianzhi offer] 6-10 questions
Google Earth engine (GEE) -- take modis/006/mcd19a2 as an example to batch download the daily mean, maximum, minimum, standard deviation, statistical analysis of variance and CSV download of daily AOD
Erik baleog and Olaf, advanced area of misc in the attack and defense world
Detailed explanation of heap sort code