剑指offer从头到尾打印链表
来源:互联网 发布:ubuntu开启root账户 编辑:程序博客网 时间:2024/06/07 13:13
输入一个链表,从尾到头打印链表每个节点的值。
1从尾到头,即先进后出,可以用栈实现
2先翻转链表,然后依次从头到尾输出
3递归实现
4利用反向迭代器输出
#include<iostream>#include<cstdio>#include<cstdlib>#include<stack>#include<vector>using namespace std;typedef struct ListNode{int value;ListNode *next;}ListNode;void printList(ListNode* pHead)//递归{ ListNode* pNode=pHead; if(pNode==NULL) return ; printList(pNode->next); cout<<pNode->value<<endl; return ;}int main(){ ListNode* a=(ListNode*)malloc(sizeof(ListNode)); a->value=10; a->next=NULL; ListNode* a1=a; for(int i=10;i<15;i++) { ListNode* b=(ListNode*)malloc(sizeof(ListNode)); b->value=i*2; b->next=NULL; a1->next=b; a1=b; } a1=a; while(a1!=NULL) { cout<<a1->value<<" "; a1=a1->next; } cout<<endl; printList(a); return 0;}//栈的实现void printList(ListNode* pHead){ ListNode* pHead=pHead; stack<ListNode*>s; if(pNode==NULL) return; while(p!=NULL) { s.push(pNode); pNode=pNode->next; } while(!s.empty()) { pNode=s.top(); cout<<pNode->value<<" "; s.pop(); } cout<<endl; return ;}//反向迭代器vector<int> printfList(ListNode* head){ vector<int> v; ListNode *p=head; while(p!=nullptr) { v,push_back(p->val); p=p->next; } return vector<int>(v.rbegin(),v.rend());}
阅读全文
0 0
- [剑指offer]从头到尾打印链表
- 剑指offer-5:从头到尾打印链表
- 剑指Offer-从头到尾打印链表
- 剑指offer—从头到尾打印链表
- 剑指Offer---从头到尾打印链表
- 牛客网-剑指offer-从头到尾打印链表
- 剑指offer 从头到尾打印链表
- 剑指offer-从头到尾打印链表
- 剑指offer从头到尾打印链表
- 《剑指Offer》面试题-从头到尾打印链表
- 《剑指offer》面试题5从头到尾打印链表
- 剑指offer面试题之从头到尾打印链表
- 剑指offer--面试题6:从头到尾打印链表
- [牛客网,剑指offer,python] 从头到尾打印链表
- 【剑指offer】面试题6:从头到尾打印链表
- 剑指offer面试题5 从头到尾打印链表(c)
- 剑指offer面试题5 从头到尾打印链表(java)
- 从头到尾打印链表
- How to Structure a Scalable And Maintainable Acceptance Test Suite
- 资源 | 亚马逊 AI 主任科学家李沐:动手学深度学习视频大全
- AI一分钟 | 马斯克:我不是中本聪,我自己的比特币都丢了;商汤科技再获阿里巴巴15亿元投资,内部消息称C轮还有更大巨头入股
- Best Cow Line (贪心)
- TMS320F28335---外部按键中断
- 剑指offer从头到尾打印链表
- 习题7
- Flink学习笔记:3、Flink分布式模式(Standalone)
- 计组
- java心得(深克隆和浅克隆)
- 【Android开发笔记】创建和使用Android库
- HTML表单
- <深入linux内核架构>--第七章 模块
- 10-爬虫攻防技术分析(分布式一)