当前位置:网站首页>[C题目]力扣206. 反转链表
[C题目]力扣206. 反转链表
2022-07-25 14:44:00 【GLC8866】
方法一:创建一个新的链表,其头结点newhead一开始为NULL,遍历旧的链表,遍历过程中若结点不为NULL,则将其头插到新链表中。每头插一次,newhead就更新一次(被cur赋值)。
struct ListNode* reverseList(struct ListNode* head)
{
struct ListNode* cur=head;
struct ListNode* newhead=NULL;//创建一个新的头结点,遍历旧链表,对其进行头插,NULL就会变成尾结点的指向
while(cur)
{
struct ListNode* tmp=cur->next;
cur->next=newhead;
newhead=cur;//更新newhead
cur=tmp;//检查旧链表下一个结点
}
return newhead;
}方法二:调转指针指向目标的方向。(感觉还是前面的方法更好)
struct ListNode* reverseList(struct ListNode* head)
{
struct ListNode* cur=head;//标记当前的结点
struct ListNode* aim=NULL;//将被cur指向的结点
if(cur==NULL)
return NULL;
while(cur->next!=NULL)//旧链表中当前位置的结点后面还有结点的话,将cur往后移一个结点,再改变cur的指向。
{
struct ListNode* tmp=cur->next;
cur->next=aim;
aim=cur;
cur=tmp;
}
cur->next=aim;
return cur;
}
边栏推荐
- 51单片机学习笔记(1)
- How to make a set of code fit all kinds of screens perfectly?
- The solution to the problem that the progress bar of ros2 installation connext RMW is stuck at 13%
- Application practice: Great integrator of the paddy classification model [paddlehub, finetune, prompt]
- Development of uni app offline ID card identification plug-in based on paddleocr
- Is it safe for Guolian securities to buy shares and open an account?
- sudo rosdep init Error ROS安装问题解决方案
- Leetcode-198- house raiding
- Gonzalez Digital Image Processing Chapter 1 Introduction
- 41 图片背景综合-五彩导航图
猜你喜欢
![[MySQL series] - how much do you know about the index](/img/d7/5045a846580be106e2bf16d7b30581.png)
[MySQL series] - how much do you know about the index

Leetcode-198- house raiding

冈萨雷斯 数字图像处理 第一章绪论

The solution to the problem that the progress bar of ros2 installation connext RMW is stuck at 13%

06、类神经网络

Filters get the data in data; Filters use data in data

The supply chain collaborative management system, a new "engine" of digitalization in machinery manufacturing industry, helps enterprises' refined management to a new level

牛客多校 E G J L

44 新浪导航 ,小米边栏 练习

Alibaba cloud installs mysql5.7
随机推荐
IP address classification, which determines whether a network segment is a subnet supernetwork
MySQL 45讲 | 06 全局锁和表锁 :给表加个字段怎么有这么多阻碍?
thymeleaf设置disabled
Browser based split screen reading
Kibana operation es
Paddlenlp之UIE关系抽取模型【高管关系抽取为例】
(original) customize a scrolling recyclerview
Resource not found: rgbd_launch 解决方案
SQL优化的一些建议,希望可以帮到和我一样被SQL折磨的你
006操作符简介
SSM framework integration, simple case
PS making and loading GIF pictures tutorial
Melody + realsense d435i configuration and error resolution
Wechat official account official environment online deployment, third-party public platform access
Several methods of spark parameter configuration
软件测试 -- 1 软件测试知识大纲梳理
The input input box of H5 page pops up the numeric keypad, which needs to support decimal points
32 chrome调试工具的使用
C language and SQL Server database technology
阿里云安装MYSQL5.7