当前位置:网站首页>C language to quickly solve the reverse linked list
C language to quickly solve the reverse linked list
2022-07-04 23:15:00 【I'm not Xiao Haiwa~~~~】
Reverse a linked list
Title Description :
Given the head node of a single linked list pHead( The header node has a value , For example, in the figure below , its val yes 1), The length is n, After reversing the linked list , Return the header of the new linked list .
Data range : 0≤n≤1000
requirement : Spatial complexity O(1) , Time complexity O(n)O(n)
For example, when entering a linked list {1,2,3} when , After reversal , The original linked list becomes {3,2,1}, So the corresponding output is {3,2,1}.
The above conversion process is shown in the figure below :
This paper introduces two methods :
(1) Reverse in place
(2) Double linked list method
( I think the two methods are similar …)
/*struct ListNode { int val; struct ListNode *next; }; */
// use c Language implementation
struct ListNode* ReverseList(struct ListNode* pHead ) {
struct ListNode *pre=NULL;//pre The pointer points to the last node of the inverted linked list , There was no reversal at first , So the point to null
struct ListNode *cur=pHead;//cur The pointer points to the first node of the linked list to be reversed , Wait for the first node to reverse , So the point to head
struct ListNode *nex=NULL;// Used to save broken chain nodes , That is, the pointer points to the second node of the linked list to be reversed
while(cur){
nex=cur->next;
cur->next=pre;// Realize the chain breaking operation
pre=cur;// Inverted node
cur=nex;// Point to the previously broken node
}
return pre;
}
// Double linked list implementation
struct ListNode* ReverseList(struct ListNode* pHead ) {
struct ListNode *newHead=NULL;
while(pHead!=NULL){
struct ListNode *temp=pHead->next; // Save the broken chain node
pHead->next=newHead;// Hang the new linked list after the visited original linked list node
newHead=pHead;
pHead=temp;
}
return newHead;
}
notes : You can also use stack to realize this problem , The first in and last out output just realizes the inversion
original text :https://blog.csdn.net/qq_46027119/article/details/124182305
边栏推荐
- 【二叉树】节点与其祖先之间的最大差值
- C语言快速解决反转链表
- Notepad++--编辑的技巧
- [Taichi] change pbf2d (position based fluid simulation) of Taiji to pbf3d with minimal modification
- Analysis of the self increasing and self decreasing of C language function parameters
- A complete tutorial for getting started with redis: redis shell
- The Chinese output of servlet server and client is garbled
- ScriptableObject
- 字体设计符号组合多功能微信小程序源码
- Talk about Middleware
猜你喜欢
随机推荐
P2181 diagonal and p1030 [noip2001 popularization group] arrange in order
JS 3D explosive fragment image switching JS special effect
OSEK标准ISO_17356汇总介绍
Redis: redis transactions
Servlet服务器端和客户端中文输出乱码问题
【ODX Studio编辑PDX】-0.2-如何对比Compare两个PDX/ODX文件
Qt加法计算器(简单案例)
The small program vant tab component solves the problem of too much text and incomplete display
Editplus-- usage -- shortcut key / configuration / background color / font size
【taichi】用最少的修改将太极的pbf2d(基于位置的流体模拟)改为pbf3d
Google Earth engine (GEE) - tasks upgrade enables run all to download all images in task types with one click
UML图记忆技巧
[roommate learned to use Bi report data processing in the time of King glory in one game]
MariaDB的Galera集群应用场景--数据库多主多活
【图论】拓扑排序
Basic knowledge of database
The solution to the lack of pcntl extension under MAMP, fatal error: call to undefined function pcntl_ signal()
How can enterprises cross the digital divide? In cloud native 2.0
P2181 对角线和P1030 [NOIP2001 普及组] 求先序排列
Observable time series data downsampling practice in Prometheus