面试题5:从尾到头打印链表

题目描述

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

题目分析

剑指Offer(纪念版)P51

代码实现

void PrintListReversingly_Iteratively(ListNode* pHead)
{
    std::stack<ListNode*> nodes;

    ListNode* pNode = pHead;
    while(pNode != NULL)
    {
        nodes.push(pNode);
        pNode = pNode->m_pNext;
    }

    while(!nodes.empty())
    {
        pNode = nodes.top();
        printf("%d	", pNode->m_nValue);
        nodes.pop();
    }
}

  

原文地址:https://www.cnblogs.com/xwz0528/p/4830579.html