当前位置:网站首页>C: Reverse linked list
C: Reverse linked list
2022-06-26 20:38:00 【The wind is as calm as a cloud】
Reverse linked lists are often used as interview questions , The answers I have read are not clear and easy to understand .
I think so : Reversing a linked list is actually taking the list out of the head in turn , Then add it to another linked list :
#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;
}
Run program output :
1 2 3
3 2 1
It can be seen that the linked list has been reversed 边栏推荐
- 论数据库的传统与未来之争之溯源溯本----AWS系列专栏
- Sword finger offer II 091 Paint the house
- Arduino UNO + DS1302利用31字节静态RAM存储数据并串口打印
- Minimum spanning tree, shortest path, topology sorting, critical path
- 数据库范式和主码的选择
- Tiktok practice ~ search page ~ scan QR code
- Database SQL statement writing
- Tiktok practice ~ sharing module ~ short video download (save to photo album)
- 股票开户的具体步骤是什么?网上开户安全吗?
- 云计算技术的发展与芯片处理器的关系
猜你喜欢

Bonne Recommandation: développer des outils de sécurité pour les terminaux mobiles

【贝叶斯分类3】半朴素贝叶斯分类器

windows系統下怎麼安裝mysql8.0數據庫?(圖文教程)

On the origin of the dispute between the tradition and the future of database -- AWS series column
Mongodb implements creating and deleting databases, creating and deleting tables (sets), and adding, deleting, modifying, and querying data

Keep alive cache component in Vue

Feitian +cipu body brings more imagination to the metauniverse

Tiktok practice ~ search page ~ scan QR code

Tiktok practice ~ homepage video ~ pull-down refresh
![[recommended collection] these 8 common missing value filling skills must be mastered](/img/ab/353f74ad73ca592a3f97ea478922d9.png)
[recommended collection] these 8 common missing value filling skills must be mastered
随机推荐
Boot indicator monitoring
Jz-062- the k-th node of binary search tree
0基础学c语言(2)
Bonne Recommandation: développer des outils de sécurité pour les terminaux mobiles
Serial port application program based on gd32
Unity——Mathf. Similarities and differences between atan and atan2
证券开户安全吗,有没有什么危险呢
710. random numbers in the blacklist
【贝叶斯分类3】半朴素贝叶斯分类器
JWT operation tool class sharing
Unity - URP get camera stack
案例描述:比赛分数管理系统,需要统计历届冠军所得比赛得分,并记录到文件中,其中系统有如下需求:- 打开系统有欢迎界面,并显示可选择的选项- 选项1:记录比赛得分- 选项2:查看往届
浏览器的垃圾回收机制
Arduino UNO + DS1302利用31字节静态RAM存储数据并串口打印
問題解决:虛擬機無法複制粘貼文件
C# 练习。类列表加记录,显示记录和清空记录
Dynamic planning 111
Muke 11. User authentication and authorization of microservices
Résolution du problème: la machine virtuelle n'a pas pu copier et coller le fichier
好物推薦:移動端開發安全工具