当前位置:网站首页>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
边栏推荐
猜你喜欢
基于ele封装下拉菜单等组件
重大危险源企业如何保障年底前完成双预防机制数字化建设任务
Windows MySQL 8 zip installation
Huawei machine test question -- deformation of hj53 Yang Hui triangle
机器学习(周志华) 第十四章概率图模型
Interim summary (Introduction + application layer + transportation layer)
Innovation and management based on Scrum
Rhcsa day 1 operation
JVM类加载器
Linux redis - redis ha sentinel cluster construction details & redis master-slave deployment
随机推荐
Step principle of logical regression in machine learning
Use of go language web Middleware
leetcode 烹飪料理
VirtualBox virtual machine (Oracle VM)
Flink stream processing engine system learning (II)
MySQL function syntax
Modify the content of MySQL + PHP drop-down box
Jupyter for local and remote access to ECS
Deploying sbert model based on torchserve < semantic similarity task >
TypeScript(1)
Efficient music format conversion tool Music Converter Pro
Airtrack cracking wireless network password (Dictionary running method)
Fashion MNIST 数据集分类训练
@Usage and difference between mapper and @ repository
The express project changes the jade template to art template
Leangoo brain map - shared multi person collaborative mind mapping tool
Shell script learning -- practical case
Mosaic Routing: implement / home / news
leetcode 烹饪料理
When using art template inheritance, compileerror: invalid or unexpected token generated