#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
边栏推荐
- 为什么现在开发一款软件的时间越来越长?
- 解决微信小程序使用switchTab跳转后页面不刷新的问题
- MES系统在工厂生产管理起到9大很重要的作用
- 如何使用Camtasia制作动态动画场景?
- 详解Git
- 第三阶段 Day16 用户模块跳转 SSO单点登录 JSONP/cors跨域方式 用户登录校检
- High quality defect analysis: let yourself write fewer bugs
- 高质量的缺陷分析:让自己少写 bug
- 零基础小白python入门——深入Python中的文件操作
- The latest version of pycharm 2020.3: pair programming, intelligent text proofreading and downloading experience
猜你喜欢
干货推荐:关于网络安全技术的专业术语,你知道多少?
拉线式位移传感器在边坡裂缝中的作用
On agile development concept and iterative development scheme
拉线位移编码器的选择,需要精量电子的指导
5分钟GET我使用Github 5 年总结的这些骚操作!
Chrome浏览器 js 关闭窗口失效解决方法
The latest version of pycharm 2020.3: pair programming, intelligent text proofreading and downloading experience
同事笔记-小程序入坑点
Set two ways of background image, and solve the mobile phone background image highly adaptive problem
超大折扣力度,云服务器 88 元秒杀
随机推荐
From next year, about 30% of the web pages will be inaccessible to older Android devices
On agile development concept and iterative development scheme
CAD tutorial cad2016 installation course
A certification and authorization solution based on. Net core - hulutong 1.0 open source
菜鸟福音,28本书籍循序渐进让你成为大牛!(附学习大纲一份)
高质量的缺陷分析:让自己少写 bug
Looking for a small immutable dictionary with better performance
5分钟GET我使用Github 5 年总结的这些骚操作!
ABBYY FineReader 15新增编辑页面布局功能
自定义室内地图在线工具
深入分析商淘多用户商城系统如何从搜索着手打造盈利点
低功耗蓝牙单芯片为物联网助力
High quality defect analysis: let yourself write fewer bugs
脑机接口先驱炮轰马斯克:“他走的是一条死胡同,说的话我一个字都不同意”
【运维思考】如何做好云上运维服务?
谈谈敏捷开发概念和迭代开发方案
Why is your money transferred? This article tells you the answer
Super discount, cloud server 88 yuan seconds
Flink的安装和测试
深入探索 Android Gradle 插件的缓存配置