### 這道題很容易可以想到后進先出的數據結構 ---- 棧,因為我們沒有 m_prev 指針。所以遍歷時,把節點壓棧就好。(這道題用遞歸代碼也很好寫)
下面是代碼實現(迭代):
```
#include <stack>
void printInverse(SList *pHead)
{
if (pHead == nullptr) {
return nullptr;
}
std::stack<SList *> nodeStack;
// 將遍歷到的節點入棧
while (pHead != nullptr) {
nodeStack.push(pHead);
pHead = pHead->m_next;
}
// 出棧打印
while (!nodeStack.empty()) {
SList *topNode = nodeStack.top();
nodeStack.pop();
std::cout << topNode->m_data << " ";
}
std::cout << endl;
}
```