#include <stdio.h>
#include <stdlib.h>
struct ListNode {
int val;
struct ListNode* next;
};
void display(struct ListNode* head)
{
struct ListNode* current = head;
while(current != NULL) {
printf("%d\n", current->val);
current = current->next;
}
}
void push(struct ListNode* head, int val)
{
struct ListNode* current = head;
while(current->next != NULL) {
current = current->next;
}
current->next = (struct ListNode*)malloc(sizeof(struct ListNode));
current->next->val = val;
current->next->next = NULL;
}
struct ListNode* traverse(struct ListNode* head)
{
if (head == NULL) {
return NULL;
}
struct ListNode* newHead = NULL;
while(head != NULL) {
struct ListNode* temp = (struct ListNode*)malloc(sizeof(struct ListNode));
temp->val = head->val;
if (newHead == NULL) {
temp->next = NULL;
} else {
temp->next = newHead;
}
newHead = temp;
head = head->next;
}
return newHead;
}
int main(int argc, char const *argv[])
{
struct ListNode* head = (struct ListNode*)malloc(sizeof(struct ListNode));
if (head == NULL) {
return 1;
}
head->val = 1;
head->next = NULL;
push(head, 2);
push(head, 3);
push(head, 4);
push(head, 5);
push(head, 6);
display(head);
struct ListNode* newHead = traverse(head);
printf("\n");
display(newHead);
}
当前位置:网站首页>单链表反转
单链表反转
2020-11-09 16:55:00 【洛奇_巴布亚】
版权声明
本文为[洛奇_巴布亚]所创,转载请带上原文链接,感谢
https://segmentfault.com/a/1190000037775371
边栏推荐
- (3)ASP.NET Core3.1 Ocelot认证
- 缓存的数据一致性
- Echart sets the spacing between columns
- EasyExcel根据筛选列导出(中间不空列,顺序可调整)
- Performance comparison of serialization tools such as Jackson, fastjson, kryo, protostuff
- High quality defect analysis: let yourself write fewer bugs
- Kubernetes-17: kubernets package management tool -- Introduction and use of Helm
- CAD tutorial cad2016 installation course
- 用微信表情翻译表白,程序员的小浪漫,赶紧Get起来!
- Restart the heap_ uaf_ hacknote
猜你喜欢

MES system is different from traditional management in industry application

CentOS查看CPU核心数及cpuinfo解析

echart 设置柱子之间的间距

5 minutes get I use GitHub's five-year summary of these complaints!

(3)ASP.NET Core3.1 Ocelot认证

Ubuntu18.04 NAT模式下配置静态IP地址 -2020.11.09

Installation and testing of Flink

Six axes of calibration service

Explore cache configuration of Android gradle plug-in

EasyExcel根据筛选列导出(中间不空列,顺序可调整)
随机推荐
Simple use of AE (after effects)
解析:C++如何实现简单的学生管理系统(源码分享)
[operation and maintenance thinking] how to do a good job in cloud operation and maintenance services?
使用Fastai开发和部署图像分类器应用
高质量的缺陷分析:让自己少写 bug
It's so easy to replace the video background with the sound and shadow
Knowledge mapping 1.1 -- starting from NER
用微信表情翻译表白,程序员的小浪漫,赶紧Get起来!
The worst hacker in history: stealing $1 billion of bitcoin without spending it for seven years, and finally being seized by the Department of justice
How about Tencent cloud amd cloud server?
. net report builder stimulsoft Reports.Net Release the latest version of v2020.5!
Explain git in detail
会展云技术解读 | 面对突发事故,APP 如何做好崩溃分析与性能监控?
干货推荐:关于网络安全技术的专业术语,你知道多少?
分享用MathType编辑字母与数学公式的技巧
Using fastai to develop and deploy image classifier application
函数计算进阶-IP查询工具开发
CAD tutorial cad2016 installation course
Which industries are suitable for enterprises to develop wechat applet?
浮点数之间的等值判断