从尾部打印头部
栈 - 实现:
void printListReversingly_Iteratively(ListNode** pHead)
{
std::stack<ListNode*> nodes;
ListNode* pNode = *pHead;
while(pNode!=nullptr){
nodes.push(pNode);
pNode = pNode->m_pNext;}
while(!nodes.empty()){
pNode = nodes.top();
printf("%d ",pNode->m_nValue);
nodes.pop();}
}
测试代码:
#include<iostream>
#include<stack>
struct ListNode{
int m_nValue;
ListNode* m_pNext;
};
ListNode* AddToTail(ListNode** pHead, int Value)
{
ListNode* pNew = new ListNode();
pNew->m_nValue = Value;
pNew->m_pNext = nullptr;
if(*pHead == NULL){
*pHead = pNew;}
else{
ListNode* pNode = *pHead;
while(pNode->m_pNext != nullptr)