Created
April 25, 2019 16:19
-
-
Save necusjz/39107507d9eb6a1736ee4e8cb98ef35f to your computer and use it in GitHub Desktop.
面试题 6:从尾到头打印链表
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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