当前位置:网站首页>C: 反转链表
C: 反转链表
2022-06-26 20:24:00 【风静如云】
反转链表经常作为面试题,看过的解答写的都不够清晰易懂。
我是这样认为的:反转链表其实就是把链表从头部依次取出,然后再加入到另一个链表:
#include <stdio.h>
struct Node{
int data;
struct Node *next;
};
struct Node* popFromHead(struct Node **head)
{
if(*head != 0 )
{
struct Node* first = *head;
*head = first->next;
return first;
}
else
{
return 0;
}
}
void pushToHead(struct Node** head, struct Node* t)
{
t->next = *head;
*head = t;
}
void printList(struct Node *head)
{
while(head != 0)
{
printf("%d ", head->data);
head = head->next;
}
printf("\n");
}
int main()
{
struct Node *head = 0;
struct Node n1, n2, n3;
n1.data = 1;
n2.data = 2;
n3.data = 3;
pushToHead(&head, &n3);
pushToHead(&head, &n2);
pushToHead(&head, &n1);
printList(head);
struct Node* pNode = 0;
struct Node* reverseList = 0;
while((pNode = popFromHead(&head)) != 0)
{
pushToHead(&reverseList, pNode);
}
printList(reverseList);
return 0;
}
运行程序输出:
1 2 3
3 2 1
可见链表被反转了边栏推荐
- SentinelResource注解详解
- vue中缓存组件keep-alive
- 黑客用机器学习发动攻击的九种方法
- Minimum spanning tree, shortest path, topology sorting, critical path
- Basic and necessary common plug-ins of vscade
- Can I open an account online? Is it safe?
- 飞天+CIPU体为元宇宙带来更大想象空间
- 0基础学c语言(2)
- Case description: the competition score management system needs to count the competition scores obtained by previous champions and record them in the file. The system has the following requirements: -
- mongoDB的三种基础备份方法
猜你喜欢

Tiktok practice ~ sharing module ~ short video download (save to photo album)

Flutter TextField详解

Tiktok practice ~ sharing module ~ generate short video QR code

Boot indicator monitoring

Basic and necessary common plug-ins of vscade

Tiktok practice ~ homepage video ~ pull-down refresh

飞天+CIPU体为元宇宙带来更大想象空间

Uni app uses canvas to draw QR code

Six necessary threat tracking tools for threat hunters

MySQL recharge
随机推荐
数据库SQL语句撰写
0基础学c语言(1)
30. concatenate substrings of all words
论数据库的传统与未来之争之溯源溯本----AWS系列专栏
回溯思路详解
抖音实战~分享模块~短视频下载(保存到相册)
c语言简单的登录
Some cold knowledge about QT database development
Development of NFT for digital collection platform
C primer plus learning notes - 3. Character IO (input / output)
Minimum spanning tree, shortest path, topology sorting, critical path
[most detailed] latest and complete redis interview (42 tracks)
Separate save file for debug symbols after strip
0基础c语言(0)
Web resource preloading - production environment practice
Feitian +cipu body brings more imagination to the metauniverse
BOM and DOM operations
mysql存储过程
0 basic C language (0)
C language simple login