逆序打印链表

来源:互联网 发布:mysql 主备热切换 编辑:程序博客网 时间:2024/04/30 07:06

用栈能很easy的解决,直接贴代码:

struct ListNode{int m_nValue;ListNode *m_pNext;};

void PrintListRevers(ListNode* pHead){    stack<ListNode*> nodes;    ListNode* pNode = pHead;    while(pNode != NULL)    {        nodes.push(pNode);        pNode = pNode->m_pNext;    }    while(!nodes.empty())    {        pNode = nodes.top();        printf("%d\t", pNode->m_nValue);        nodes.pop();    }}


0 0