//反序输出链表中的值采用的方法是顺序遍历链表,将读取到的数存到栈中,再输出的办法。
void PrintListReversingly_Iteratively(ListNode* pHead){ stack<ListNode*> nodes; ListNode *pNode=pHead; while(pNode!=nullptr){ nodes.push(pNode); pNode=pNode->m_pNext; } while(!nodes.empty()){ pNode = nodes.top(); cout<<pNode->m_nValue; nodes.pop(); }}//既然考虑到了用栈来进行倒序输出,自然也就想到了通过递归方式进行输出,因为递归本身就是一个栈结构基于这样的思路//我要做的就是访问到一个节点时先递归的输出它后面的节点,再输出它自己,这样就完成了倒序输出void PrintListReversingly_Recursizvely(ListNode* pHead){ if(pHead!=nullptr){ if(pHead->m_pNext!=nullptr) PrintListReversingly_Recursizvely(pHead->m_pNext); cout<<pHead->m_nValue; }}void Test(ListNode * pHead){ PrintListReversingly_Iteratively(pHead); cout<<endl; PrintListReversingly_Recursizvely(pHead);}