#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
边栏推荐
- 5 minutes get I use GitHub's 5-year summary of these operations!
- 5 minutes get I use GitHub's 5-year summary of these operations!
- Configure static IP address in ubuntu18.04 NAT mode -2020.11.09
- 浮点数之间的等值判断
- It's so easy to replace the video background with the sound and shadow
- Mit6.824 distributed system course translation & learning notes (3) GFS
- I heard that you changed your registered residence overnight. How can you help yourself if you work like ping?
- [share] interface tests how to transfer files in post request
- Using fastai to develop and deploy image classifier application
- Easyexcel exports according to the filter column (not empty in the middle, the order can be adjusted)
猜你喜欢

明年起旧版本安卓设备将无法浏览大约30%的网页

拉线式位移传感器在边坡裂缝中的作用

Installation and testing of Flink

On agile development concept and iterative development scheme

Toolkit Pro helps interface development: shorten the project development cycle and quickly realize GUI with modern functional area style

Super discount, cloud server 88 yuan seconds

CentOS view the number of CPU cores and cpuinfo analysis

Why is your money transferred? This article tells you the answer

Full stack technology experience tells you: how much does it cost to develop a mall small program?

Mit6.824 distributed system course translation & learning notes (3) GFS
随机推荐
Flash Book curd project
Kubernetes V1.19.3 kubeadm 部署笔记(中)
Looking for a small immutable dictionary with better performance
2020中国电信终端技术与标准论坛上专家解读四大热门产业
Gesture switch background, let live with goods more immersive
Easyexcel exports according to the filter column (not empty in the middle, the order can be adjusted)
MIT6.824分布式系统课程 翻译&学习笔记(三)GFS
高质量的缺陷分析:让自己少写 bug
MES system plays an important role in the factory production management
解决微信小程序使用switchTab跳转后页面不刷新的问题
CentOS查看CPU核心数及cpuinfo解析
How about Tencent cloud amd cloud server?
Ubuntu18.04 NAT模式下配置静态IP地址 -2020.11.09
浮点数之间的等值判断
程序模拟感知机算法(梯度下降法、sklearn.linear_model中perception方法)
Help enterprises to get rid of difficulties, famous enterprises return home Engineers: success depends on it!
函数计算进阶-IP查询工具开发
echart 设置柱子之间的间距
第三阶段 Day16 用户模块跳转 SSO单点登录 JSONP/cors跨域方式 用户登录校检
ABBYY FineReader 15新增编辑页面布局功能