反转单链表
来源:互联网 发布:东华软件金融部 编辑:程序博客网 时间:2024/05/20 07:36
csdn第一篇博客,之前的博客一直放在自己的网站上,可是现在觉得wordpress好不方便,还是在CSDN上写吧,从现在开始记录找工作的点点滴滴。
反转单链表,超级热门的面试题,反正我是看到不下十次了,有递归和非递归两种方法
第一种:非递归方法
#include <iostream>#include<cstdio>#include<cstdlib>using namespace std;/*链表结构体*/struct Node{ int data; struct Node* next;};static void reverse(struct Node** head_ref){ struct Node* prev =NULL; struct Node* current =*head_ref; struct Node* next; while(current!=NULL){ next =current->next; current->next=prev; prev=current; current=next; } *head_ref=prev;}void push(struct Node** head_ref,int new_data){ struct Node* new_node= (struct Node*)malloc(sizeof(struct Node)); new_node->data=new_data; new_node->next=(*head_ref); (*head_ref)=new_node;}void printList(struct Node *head){ struct Node *temp=head; while(temp!=NULL){ cout<<temp->data<<" "; temp=temp->next; }}int main(){ struct Node *head=NULL; push(&head,2); push(&head,4); push(&head,6); push(&head,8); printList(head); reverse(&head); cout<<endl; printList(head); return 0;}
阅读全文
0 0
- 单链表反转
- 单链表反转
- 单链表反转
- 反转单链表
- 单链表反转
- 反转单链表
- 单链表-反转
- 单链表反转
- 单链表反转
- 单链表反转
- 单链表反转
- 单链表反转
- 单链表反转
- 单链表反转
- 单链表反转
- 单链表反转
- 反转单链表
- 单链表反转
- Mac电脑上配置Java开发环境
- log4js 日志模块
- 达夫设备(Duff`s Device)减少迭代次数
- Angular CLI 使用教程指南参考
- LeetCode——105. Construct Binary Tree from Preorder and Inorder Traversal
- 反转单链表
- 简单线程池实现原理
- Codeforces Round #432 (Div. 2) B. Arpa and an exam about geometry(数学水题)
- 几种常见加密算法解析及使用
- UVALive 4764 Bing it(dp)
- Linux的system()和popen()差异
- HTML表格颜色搭配+柱状图颜色搭配+HTML柱状图实现
- Windows Message Queue
- 引用参数传递