从尾到头打印链表

//
void printList(ListNode* pHead)
{
    std::stack<ListNode*> printStack;
    ListNode* pTemp = pHead;

    while (pTemp != nullptr)
    {
        printStack.push(pTemp);
        pTemp = pTemp->pNext;
    }
    while (!printStack.empty())
    {
        cout << printStack.top()->nValue;
        printStack.pop();
    }
    return;
}
//递归
void printList(ListNode* pHead)
{
    if (pHead == NULL)
        return;
    if (pHead->pNext != NULL)
    {
        printList(pHead->pNext);   //当链表长度过长时,会导致函数调用层级过深,函数调用栈溢出
    }
    cout << pHead->nValue;
    return;
}
原文地址:https://www.cnblogs.com/yapp/p/14310127.html