当前位置:网站首页>[leetcode ladder] linked list · 206 reverse linked list
[leetcode ladder] linked list · 206 reverse linked list
2022-07-23 22:53:00 【kikokingzz】

Title Description :
Here's the head node of the list
head, Please reverse the list , And return the inverted linked list .
Example 1:

Input :head = [1,2,3,4,5]
Output :[5,4,3,2,1]Example 2:

Input :head = [1,2]
Output :[2,1]Example 3:
Input :head = []
Output :[]Topic link :
Their thinking :
Fool method 1: Create a new linked list and go through it one by one
This operation should be the easiest to think of , But it is also the most complex . We need a pointer to traverse one by one from back to front , But the single linked list cannot be traversed from back to front , So we need to make a pointer traverse from front to back to the last , Then insert the node into the new linked list ; Then traverse from front to back to the penultimate , Then insert it into the single linked list ···· The time complexity of such an operation should be :
namely , The time complexity of this algorithm will reach
, Is very unsatisfactory , So this method , We directly eliminate !
Conventional method 2: Use three pointers to flip
Next, we think of directly operating the original linked list , Flip the arrows one by one from front to back ? We don't need to flip the numbers , Instead, try to flip the arrows between nodes !
At this time, we need to use three pointers to flip , The logical idea is to use pointers n1 As the header of the new linked list ; The pointer n2 Used to change its pointing node next, Make the original linked list node and n1 Connected to a ; The pointer n3 Is used to keep n2 The next node address , The main logic operations are as follows :
struct ListNode* reverseList(struct ListNode* head){ struct ListNode* n1=NULL; struct ListNode* n2=head; if(head==NULL) return NULL; // If it is an empty linked list , Direct output NULL while(n2) //n2 It's empty , explain n2 Finished traversing the linked list { struct ListNode* n3=n2->next; //n3 Point to the first node of the original linked list ( May be NULL) n2->next=n1; // take n2 Of the node currently pointed to next Point to n1 n1=n2; // Put the header pointer of the new linked list n1 to update n2=n3; //n2 Continue to take a step backwards } return n1; }
To sum up : This problem uses three pointers to operate in a regular way , A pointer is used as the header pointer of the new linked list , A pointer is used to traverse the current linked list , The other is used to save the next node location of the current node .
Conventional method 3: Insert the linked list elements into the header
This operation is also very smart , We use a pointer to traverse from front to back , Another pointer performs header insertion on each node , The third pointer is used to reserve the address of the next node , This naturally makes the original single linked list reverse .
struct ListNode* reverseList(struct ListNode* head){ struct ListNode* newHead=NULL; struct ListNode* cur=head; while(cur) // When cur It's empty time , explain cur The linked list has been traversed { struct ListNode* next=cur->next; //cur Every step back , All set up one next preservation cur Next position of cur->next=newHead; // take cur Point to the first node of the new linked list ( May be NULL) newHead=cur; // Point the head pointer of the linked list to cur cur=next; //cur Take a step back } return newHead; }
To sum up : Flexibly use some basic operations when we first learn single linked list , It is the first step for us to think about every linked list problem , Some topics about linked lists come to the essence , But it will be “ Additions and deletions ” It's just a combination of operations .
边栏推荐
- Tiktok launches multilingual subtitles and translation tools
- Lixia action 2022 Yuanqi digital round table forum will be launched soon
- Stm32+esp8266+mqtt protocol connects Alibaba cloud Internet of things platform
- Extract any page number in PDF file with itextpdf
- Wangxuegang video coding -- mediacodec coding and decoding
- 小说里的编程 【连载之十八】元宇宙里月亮弯弯
- 【Unity3D日常BUG】Unity3D解决“找不到类型或命名空间名称“XXX”(您是否缺少using指令或程序集引用?)”等问题
- The ultimate experiment of OSPF -- learn the example of OSPF century template
- How can I open an account to buy financial products with a 6% income?
- 激光雷达点云数据录制的rosbag文件转换成csv文件
猜你喜欢

As a developer, you have to know the three performance testing tools JMeter, API and jmh user guide
![Use of [golang learning notes] package](/img/be/71cb066f44ce4bdab13c79c2da2652.png)
Use of [golang learning notes] package

Microsoft SQL Server数据库语言及功能使用(十三)

The role of physical layer, link layer, network layer, transport layer and application layer of tcp/ip model of internet protocol stack

Introduction and project development of MVVM and mvvmlight (I)

el-select下拉框多选远程搜索反显

浅析基于NVR技术的视频能力与未来发展趋势

海外资深玩家的投资建议(3) 2021-05-04

糖尿病遗传风险检测挑战赛Baseline
About synchronizing data from computer to mobile
随机推荐
Is Ping An Securities' low commission account opening link safe? How to handle low commission
TAP 系列文章4 | 基于 Backstage 的 TAP 开发者门户
Matlab小波工具箱导入信号出错(doesn‘t contain one dimensional Singal)
达梦数据库tools包中的工具(操作达梦数据库)
unity visual studio2019升级到2022版本(扔掉盗版红渣)
Tap series article 7 | easy to manage pipeline configuration
ES6箭头函数的使用
EasyNVR平台如何关闭匿名登录?
MySQL index transaction
Relevant interfaces of [asp.net core] option mode
Internet协议栈 TCP/IP模型 物理层、链路层、网络层、传输层、应用层的作用
思源笔记的字体比其他的编辑器(Atom,VSC,sublime)内字体渲染更细更淡
Series of articles | the way to advance the microservice architecture in the cloud native era - best practices of microservice splitting
Wangxuegang video coding -- mediacodec coding and decoding
Crazy bull market, where to go in the second half? 2021-04-30
Array - 977. Square of ordered array
年化收益率6%的理财产品
记忆化搜索 - DP
Excel password related
The ultimate experiment of OSPF -- learn the example of OSPF century template
https://leetcode.cn/problems/reverse-linked-list/
, Is very unsatisfactory , So this method , We directly eliminate !



