题目:输入一个链表的头结点,从尾到头反过来打印出每个节点的值。

成都创新互联公司是专业的沁水网站建设公司,沁水接单;提供网站制作、做网站,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行沁水网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
方法1:使用栈
/*
链表节点定义如下:
struct ListNode
{
int _data;
ListNode* _next;
};
*/
void PrintListTailToHead(ListNode* phead)
{
assert(phead);
stack s;
ListNode* cur = phead;
while (cur)
{
s.push(cur);
cur = cur->_next;
}
while (!s.empty())
{
cout<_data<<" ";
s.pop();
}
cout<方法2:递归
void PrintListTailToHead(ListNode* phead)
{
if (phead == NULL)
{
return;
}
PrintListTailToHead(phead->_next);
cout<_data<<" ";
}
新闻名称:面试题:从尾到头打印链表
浏览路径:http://cdysf.com/article/ihdopg.html