当前位置:网站首页>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
边栏推荐
- Efficient music format conversion tool Music Converter Pro
- Modification du contenu de la recherche dans la boîte déroulante par PHP + MySQL
- TypeScript(1)
- Renesas electronic MCU RT thread development and Design Competition
- Solve the problem that the registered Google email Gmail mobile number cannot be used for verification
- [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
- Log cutting - build a remote log collection server
- [XJTU computer network security and management] Lecture 2 password technology
- Planning code ROS migration POMDP prediction planning (I)
- 工业互联网+危化安全生产综合管理平台怎样建
猜你喜欢
![[hcip] detailed explanation of six LSAS commonly used by OSPF](/img/31/3b92d42d16a056bf9db9e24471cefd.jpg)
[hcip] detailed explanation of six LSAS commonly used by OSPF

Flink stream processing engine system learning (II)

Fashion MNIST 数据集分类训练

Flink stream processing engine system learning (I)

Android 高阶面试必问:全局业务和项目的架构设计与重构

ROP Emporium x86_ 64 7 ~ 8 questions

国产轻量级看板式Scrum敏捷项目管理工具

Interpretation of the future development of smart agriculture

Interim summary (Introduction + application layer + transportation layer)

Download the genuine origin Pro 2022 tutorial and how to activate it
随机推荐
Redis data server / database / cache (2022)
MySQL / SQL Server判断表或临时表存在则删除
The shell monitors the depth of the IBM MQ queue and scans it three times in 10s. When the depth value exceeds 5 for more than two times, the queue name and depth value are output.
php+mysql對下拉框搜索的內容修改
Winsock programming interface experiment: Ping
Cuisine leetcode
JZ35 复杂链表的复制
《信息系统项目管理师总结》第四章 项目成本管理
Essential qualities of advanced programmers
[hcip] detailed explanation of six LSAS commonly used by OSPF
Log4j知识点记录
Learn regular expression options, assertions
Centos7 install MySQL 8 0
Day 3 of learning rhcsa
Jz76 delete duplicate nodes in linked list
Decision tree principle of machine learning
Jupyter for local and remote access to ECS
Linux redis - redis ha sentinel cluster construction details & redis master-slave deployment
Servlet template engine usage example
1215_ Hello world used by scons