当前位置:网站首页>LeetCode [206. Reverse linked list] (1)
LeetCode [206. Reverse linked list] (1)
2022-08-07 08:56:00 【#tangjieyuan】
题目描述
题目分析
This is a popular question,Lots of school recruits,Written exams are frequent.
After a brief reading of the question, we basically understand what it is asking us to achieve
就是Invert the linked list, 如图:
Since the title does not require time complexity and space complexity,So let's get started
解决方案
思路一:翻指针方向
Start with three pointers,让n1指向NULL,n2指向链表第一个结点,n3The second node of the linked list,如图:
Generally speaking, the procedure for repeating the process is divided into three steps:
- 初始条件
Our initial condition is to use three pointers,respectively point to null,The first node and the second node.- 迭代过程
第一步:让n2指向n1,This step realizes the flip of the first node of the linked list
第二步:Let the above three pointers move back one node respectively(将n1指向n2;再让n2指向n3;最后使n3指向它的下一个结点n3->next).- 结束条件
The first step in our iterative process is to let n2去翻转,then whenn2指向NULL的时候截止.
过程如图:
思路一代码:
struct ListNode* reverseList(struct ListNode* head){
if(head==NULL)
return NULL;
//初始条件
struct ListNode*n1=NULL,*n2=head,*n3=n2->next;
while(n2!=NULL)//结束条件
{
//迭代过程
n2->next=n1;
n1=n2;
n2=n3;
if(n3)
n3=n3->next;
}
return n1;
}
思路二:头插法
顾名思义,Head plug method is to follow the idea of head plug.Create a new linked list first,Just put the nodes in the old linked list into the new linked list by head-plugging.
第一步: 定义一个指针指向head,在定义一个新链表newHead.
第二步:定义一个next用来记录curthe latter node,将curThe node pointed to is placednewHead前面,再将newHead指向cur,cur再向后走.
旧链表head的流程图:
新链表newHead的流程图:
思路二代码:
struct ListNode* reverseList(struct ListNode* head){
struct ListNode*cur=head;
struct ListNode*newHead=NULL;
while(cur!=NULL)
{
struct ListNode*next=cur->next;
//头插
cur->next=newHead;
newHead=cur;
cur=next;
}
return newHead;
}
边栏推荐
猜你喜欢

LVS+Keepalived高可用群集部署

压缩冗余信息

(一)UPF之电源网络(Supply_port、Supply_net、Supply_set)

The baud rate of STM32 is wrong. The baud rate we want to set is 9600, but the actual baud rate is 14400. Why is this?

2022华数杯数学建模-在线文档

#yyds干货盘点#【愚公系列】2022年08月 Go教学课程 004-Go代码注释

The principle and source code of redis - the principle of master-slave replication

openharmony萌新贡献指南

Jenkins configures automatic packaging

redis的原理和源码-redis各数据类型的编码格式和数据结构SDS、list、dict、zskiplist、intset、ziplist、quicklist、listpack、rax、stream
随机推荐
jenkins配置自动打包
基于密码芯片的 DDR 加速器的设计与实现
2022 TV cup mathematical modeling - online documentation
力拓信创生态,博睿数据多款产品获得东方通与达梦数据库产品兼容互认证明
The third bullet of FPGA development: button control LED experiment
你如何看待抖音的中视频伙伴计划的?
Why do I say: curry = = closure + recursion?
架构师杂谈【摘抄】
Transport layer (UDP protocol, TCP protocol three-way handshake, four-way wave)
随笔-那些快乐的日子
今日睡眠质量记录74分
rest client: a lightweight vscode plugin for api requests
day2-机器学习-聚类
LVS+Keepalived高可用群集部署
The principle and source code of redis-sentinel sentinel principle and source code analysis (on)
压缩冗余信息
Arthas 使用报错
Model fine-tuning transfer learning Finetune method Daquan
岛屿的最大面积
6.PHP函数、$GET和$POST变量






