当前位置:网站首页>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
边栏推荐
- Ffmpeg quick clip
- S32 Design Studio for ARM 2.2 快速入门
- [crawler] XPath for data extraction
- Redis入门完整教程:有序集合详解
- Google collab trample pit
- HMS core unified scanning service
- Editplus-- usage -- shortcut key / configuration / background color / font size
- A mining of edu certificate station
- Excel 快捷键-随时补充
- Redis getting started complete tutorial: Key Management
猜你喜欢

【剑指Offer】6-10题

【js】-【排序-相关】-笔记

一次edu证书站的挖掘

Analysis of the self increasing and self decreasing of C language function parameters

Galera cluster of MariaDB - dual active and dual active installation settings

Google Earth engine (GEE) - globfire daily fire data set based on mcd64a1

Redis introduction complete tutorial: List explanation

Observable time series data downsampling practice in Prometheus

Redis introduction complete tutorial: detailed explanation of ordered collection

Redis入门完整教程:事务与Lua
随机推荐
Basic knowledge of database
Redis getting started complete tutorial: hash description
Application of machine learning in housing price prediction
MySQL数据库备份与恢复--mysqldump命令
HMS core unified scanning service
Ffmpeg quick clip
Google Earth engine (GEE) - globfire daily fire data set based on mcd64a1
头文件重复定义问题解决“C1014错误“
Pagoda 7.9.2 pagoda control panel bypasses mobile phone binding authentication bypasses official authentication
phpcms付费阅读功能支付宝支付
Qualcomm WLAN framework learning (30) -- components supporting dual sta
Stm32 Reverse Introduction to CTF Competition Interpretation
Question brushing guide public
EditPlus--用法--快捷键/配置/背景色/字体大小
PS style JS webpage graffiti board plug-in
【js】-【动态规划】-笔记
Photoshop batch adds different numbers to different pictures
Redis入门完整教程:键管理
One of the commonly used technical indicators, reading boll Bollinger line indicators
该如何去选择证券公司,手机上开户安不安全