#include<iostream>
#include<vector>
#include<string>
#include<stack>
using namespace std;
struct ListNode
{
int nValue;
ListNode* pNext;
};
void Print(ListNode *head) {
stack<int> m;
ListNode *p = head;
while (p!=NULL)
{
m.push(p->nValue);
p=p->pNext;
}
while (!m.empty())
{
cout << m.top() << " ";
m.pop();
}
cout << endl;
}//方法一
void Print2(ListNode *head) {
if (head != NULL) {
if (head->pNext != NULL) Print2(head->pNext);
cout<<head->nValue<<" ";
}
}//方法二
int main()
{
ListNode *p = new ListNode();
p->nValue = 1;
p->pNext= new ListNode();
p->pNext->nValue = 2;
p->pNext->pNext= new ListNode();
p->pNext->pNext->nValue = 3;
Print(p);
Print2(p);
return 0;
}
剑指offer6
最新推荐文章于 2020-10-23 16:01:11 发布