#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
边栏推荐
- 树莓派内网穿透建站与维护,使用内网穿透无需服务器
- 高质量的缺陷分析:让自己少写 bug
- Revealing the logic of moving path selection in Summoner Canyon?
- 知识图谱描边1.1——从NER上手
- 会展云技术解读 | 面对突发事故,APP 如何做好崩溃分析与性能监控?
- Mobile security reinforcement helps app achieve comprehensive and effective security protection
- 使用Fastai开发和部署图像分类器应用
- Booker · apachecn programming / back end / big data / AI learning resources 2020.11
- Method of conversion between JS character and ASCII code
- Why is your money transferred? This article tells you the answer
猜你喜欢
知识图谱描边1.1——从NER上手
轻松构建高颜值应用界面,这些内置图标多重要!
Talking about PHP file fragment upload from a requirement improvement
缓存的数据一致性
Program simulation perceptron algorithm (gradient descent method sklearn.linear_ Perception method in model)
超简单集成华为系统完整性检测,搞定设备安全防护
Configure static IP address in ubuntu18.04 NAT mode -2020.11.09
Installation and testing of Flink
On agile development concept and iterative development scheme
It's so easy to replace the video background with the sound and shadow
随机推荐
Cad2016 software installation tutorial
Application of pull wire displacement sensor in slope cracks
MES system is different from traditional management in industry application
手势切换背景,让直播带货更加身临其境
Kubernetes-17:Kubernets包管理工具—&gt;Helm介绍与使用
Gesture switch background, let live with goods more immersive
程序模拟感知机算法(梯度下降法、sklearn.linear_model中perception方法)
Flink的安装和测试
微服务框架 Go-Micro 集成 Nacos 实战之服务注册与发现
函数计算进阶-IP查询工具开发
MIT6.824分布式系统课程 翻译&学习笔记(三)GFS
第三阶段 Day16 用户模块跳转 SSO单点登录 JSONP/cors跨域方式 用户登录校检
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!
In depth analysis of the multi-user shopping mall system from search to create a profit point
自定义室内地图在线工具
MES system plays an important role in the factory production management
Solve the problem that the page does not refresh after the wechat applet uses switchtab to jump
全栈技术实践经历告诉你:开发一个商城小程序要多少钱?
Colleague notes - small program entry point
函数计算进阶-IP查询工具开发