#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
边栏推荐
- Looking for a small immutable dictionary with better performance
- 我在传统行业做数字化转型(1)预告篇
- CentOS查看CPU核心数及cpuinfo解析
- 5 minutes get I use GitHub's five-year summary of these complaints!
- MIT6.824分布式系统课程 翻译&学习笔记(三)GFS
- (3) ASP.NET Core3.1 Ocelot certification
- Super discount, cloud server 88 yuan seconds
- [share] interface tests how to transfer files in post request
- Function calculation advanced IP query tool development
- Custom indoor map online tool
猜你喜欢
Restart the heap_ uaf_ hacknote
High quality defect analysis: let yourself write fewer bugs
同事笔记-小程序入坑点
Do you think it's easy to learn programming? In fact, it's hard! Do you think it's hard to learn programming? In fact, it's very simple!
SEO solution development, how to break away from the shadow of the bow?
Which industries are suitable for enterprises to develop wechat applet?
Flink的安装和测试
(3)ASP.NET Core3.1 Ocelot认证
MES系统在工厂生产管理起到9大很重要的作用
如何设计并实现存储QoS?
随机推荐
EasyExcel根据筛选列导出(中间不空列,顺序可调整)
Mobile security reinforcement helps app achieve comprehensive and effective security protection
5 minutes get I use GitHub's 5-year summary of these operations!
CentOS view the number of CPU cores and cpuinfo analysis
Configure static IP address in ubuntu18.04 NAT mode -2020.11.09
How to design and implement storage QoS?
Data consistency of cache
使用Fastai开发和部署图像分类器应用
详解Git
MES system plays an important role in the factory production management
QML Repeater
5分钟GET我使用Github 5 年总结的这些骚操作!
谷粒商城学习笔记,第五天:ES全文检索
Simple use of AE (after effects)
解决微信小程序使用switchTab跳转后页面不刷新的问题
Echart sets the spacing between columns
第三阶段 Day16 用户模块跳转 SSO单点登录 JSONP/cors跨域方式 用户登录校检
5 minutes get I use GitHub's 5-year summary of these operations!
深入探索 Android Gradle 插件的缓存配置
CentOS查看CPU核心数及cpuinfo解析