【面试】4.单链表的反向
来源:互联网 发布:apm测试软件 编辑:程序博客网 时间:2024/06/10 12:52
参考经典博文:链表逆序
给定单链表的头指针Head,可以将链表反向,一种方法利用指针直接赋值,一种借助stack先进后出的特性
#include<iostream>#include<stack>using namespace std;struct node{ int i; node* next;};void reverse1(node** head){ node* p1; node* p2; p1=*head; p2=p1->next; (*head)->next=NULL; while(p2) ...{ p1=p2->next; p2->next=*head; *head=p2; p2=p1; }}void reverse2(node** head){ stack<node*> S; node* temp=*head; while(temp) { S.push(temp); temp=temp->next; } *head=S.top(); S.pop(); temp=*head; while(!S.empty()) { temp->next=S.top(); S.pop(); temp=temp->next; temp->next=NULL; }}int main(){ node* head=new node; head->i=0; head->next=NULL; node* temp=head; int i; for(i=1;i<10;++i) { temp->next=new node; temp=temp->next; temp->i=i; temp->next=NULL; } temp=head; while(temp) { cout<<temp->i<<endl; temp=temp->next; } reverse1(&head); temp=head; while(temp) { cout<<temp->i<<endl; temp=temp->next; } reverse2(&head); temp=head; while(temp) { cout<<temp->i<<endl; temp=temp->next; } return 0;}
- 【面试】4.单链表的反向
- 单链表的反向
- 单链表的反向
- 单链表的反向输出
- 单链表的反向
- 字符串,单链表的反向实现
- 单链表的正向排序,反向排序
- [面试] 递归 反向输出链表 ,定义一个求字符串长度的函数, 反向输出字符串. 不能再函数内部定义变量.
- 查出反向木马的反向连接域名
- 查出反向木马的反向连接域名
- 反向连接的SHELL
- 反向工程的工具
- PowerDesigner的反向工程
- 反向神经网络的缺点
- 反向代理的设计
- powerdesigner的反向工程
- 直方图的反向投影
- TreeMap的 反向遍历
- key event (keydown keyup...)
- js兼容性
- Android最佳实践之:StrictMode介绍
- 怎么处理警告:编码 GBK 的不可映射字符和ant的一些错误
- 励志故事:从辍学到月薪一万
- 【面试】4.单链表的反向
- json格式
- C++进行质数判断
- 黑马程序员-java多态学习笔记
- TClientDataSet[20]: 使用 Blob 字段
- 1257: [CQOI2007]余数之和sum(数学分段统计)
- 分集增益
- 万普SDK与strictMode 冲突。
- mysql 复习