当前位置:网站首页>Reverse a linked list < difficulty coefficient >
Reverse a linked list < difficulty coefficient >
2022-04-23 02:51:00 【Hua Weiyun】
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 :[ ]

🧷 platform :Visual studio 2017 && windows
The core idea :
Ideas 1, Adjust the direction of the node pointer

Ideas 2, Head insertion

#include<stdio.h>#include<stdlib.h>typedef int SLTDataType;struct ListNode{ int val; struct ListNode *next;};// Ideas 1struct ListNode* reverseList1(struct ListNode* head){ // Empty list if(head == NULL) return head; struct ListNode* n1, *n2, *n3; n1 = NULL; n2 = head; n3 = head->next; while(n2) { n2->next = n1; n1 = n2; n2 = n3; // The last time I came in ,n3 Null pointer if(n3) n3 = n3->next; } return n1;}// Ideas 2struct ListNode* reverseList2(struct ListNode* head){ struct ListNode *newnode, *cur, *next; newnode = NULL; cur = head; while(cur) { next = cur->next; cur->next = newnode; newnode = cur; cur = next; } return newnode;}int main(){ struct ListNode* n1 = (struct ListNode*)malloc(sizeof(struct ListNode)); n1->val = 1; struct ListNode* n2 = (struct ListNode*)malloc(sizeof(struct ListNode)); n2->val = 2; struct ListNode* n3 = (struct ListNode*)malloc(sizeof(struct ListNode)); n3->val = 3; struct ListNode* n4 = (struct ListNode*)malloc(sizeof(struct ListNode)); n4->val = 4; struct ListNode* n5 = (struct ListNode*)malloc(sizeof(struct ListNode)); n5->val = 5; n1->next = n2; n2->next = n3; n3->next = n4; n4->next = n5; n5->next = NULL; reverseList1(n1); reverseList2(n1); return 0;}
版权声明
本文为[Hua Weiyun]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220737087075.html
边栏推荐
- Using go language to build web server
- leangoo脑图-共享式多人协作思维导图工具分享
- 工业互联网+危化安全生产综合管理平台怎样建
- 【工欲善其事必先利其器】论文编辑及文献管理(Endnote,Latex,JabRef ,overleaf)资源下载及使用指南
- leetcode 烹饪料理
- Planning code ROS migration POMDP prediction planning (I)
- Linux Redis——Redis 数据库缓存服务
- First knowledge of C language ~ branch statements
- OCR recognition PDF file
- Shell script learning notes - regular expressions
猜你喜欢

windows MySQL8 zip安装

Interpretation of the future development of smart agriculture

Airtrack cracking wireless network password (Dictionary running method)

Shell script learning notes - regular expressions

How can enterprises with major hazard installations ensure the completion of the digital construction task of double prevention mechanism by the end of the year

机器学习(周志华) 第十四章概率图模型

谷雨
![[hcip] detailed explanation of six LSAS commonly used by OSPF](/img/31/3b92d42d16a056bf9db9e24471cefd.jpg)
[hcip] detailed explanation of six LSAS commonly used by OSPF

Linux redis - redis ha sentinel cluster construction details & redis master-slave deployment

Kubernetes - Introduction to actual combat
随机推荐
JVM runtime data area (I)
[if you want to do a good job, you must first use its tools] Guide for downloading and using paper editing and document management (endnote, latex, jabref, overflow) resources
1215_ Hello world used by scons
Step principle of logical regression in machine learning
How to solve the complexity of project document management?
JVM运行时数据区(一)
Les derniers noeuds K de la liste jz22
leangoo脑图-共享式多人协作思维导图工具分享
Codeforces round 784 (Div. 4) (a - H)
Yes, from today on, our fans can participate in Netease data analysis training camp for free!
[unity3d] rolling barrage effect in live broadcasting room
Efficient music format conversion tool Music Converter Pro
Mosaic Routing: implement / home / news
Machine learning (Zhou Zhihua) Chapter 14 probability graph model
JZ35 replication of complex linked list
Rhcsa second day operation
Specific field information of MySQL export table (detailed operation of Navicat client)
Win view port occupation command line
php+mysql对下拉框搜索的内容修改
【工欲善其事必先利其器】论文编辑及文献管理(Endnote,Latex,JabRef ,overleaf)资源下载及使用指南