Skip to content

Instantly share code, notes, and snippets.

@necusjz
Created April 25, 2019 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save necusjz/39107507d9eb6a1736ee4e8cb98ef35f to your computer and use it in GitHub Desktop.
Save necusjz/39107507d9eb6a1736ee4e8cb98ef35f to your computer and use it in GitHub Desktop.
面试题 6:从尾到头打印链表
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\t", pNode->m_nValue);
nodes.pop();
}
}
void PrintListReversingly_Recursively(ListNode *pHead) {
if(pHead != nullptr) {
if(pHead->m_pNext != nullptr) {
PrintListReversingly_Recursively(pHead->m_pNext);
}
printf("%d\t", pHead->m_nValue);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment