#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define PB push_back
#define MP make_pair
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,l,h) for(int i=(l);i<=(h);++i)
#define DWN(i,h,l) for(int i=(h);i>=(l);--i)
#define CLR(vis,pos) memset(vis,pos,sizeof(vis))
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LINF 1000000000000000000LL
#define eps 1e-8
typedef long long ll;
struct ListNode{
int val;
ListNode *next;
};
int main()
{
int tmp;
ListNode *p=new ListNode;
ListNode *head=new ListNode;
p=head;
while(cin>>tmp && tmp!=-1){
ListNode *s=new ListNode;
s->val=tmp;
p->next=s;
p=s;
}
p->next=NULL;
head=head->next;
stack<ListNode*> stk;
p=head;
while(p!=NULL){
stk.push(p);
p=p->next;
}
while(!stk.empty()){
p=stk.top();
printf("%d\n",p->val);
stk.pop();
}
return 0;
}
剑指offer 05: 从尾到头打印链表
最新推荐文章于 2023-05-30 16:51:11 发布