两种方式,递归和非递归
//从尾到头打印链表
void PrintListReversingly_Iteratively(ListNode* pHead)
{
stack<ListNode*> nodes;
ListNode* pNode = pHead;
while (pNode != nullptr)
{
nodes.push(pNode);
pNode = pNode->L_next;
}
while (!nodes.empty())
{
pNode = nodes.top();
cout << pNode->L_value << " ";
nodes.pop();
}
}
void PrintListReversingly_Recursively(ListNode* pHead)
{
if (pHead != nullptr)
{
if (pHead->L_next != nullptr)
{
PrintListReversingly_Recursively(pHead->L_next);
}
cout << pHead->L_value << " ";
}
}