标签:test return bsp reverse 使用 rev poi color turn
有两种使用方法,可以用递归和非递归:
递归代码:
TestPointer.h
struct ListNode { int m_nValue; ListNode *m_pNext; }; class TestPointer { public: ListNode* reverseListNode(ListNode *pHead); void printNode(ListNode *pNode); private: ListNode* reverseCode(ListNode *pNode); };
TestPointer.cpp
ListNode * TestPointer::reverseCode(ListNode *pNode) { if (pNode->m_pNext == nullptr) return pNode; ListNode *pReverseHead= reverseCode(pNode->m_pNext); ListNode *pCurNode= pReverseHead; while(pCurNode->m_pNext != nullptr) { pCurNode = pCurNode->m_pNext; } pCurNode->m_pNext = pNode;
pNode->m_pNext = nullptr;
return pReverseHead; } void TestPointer::printNode(ListNode *pNode) { while(pNode != nullptr) { cout << pNode->m_nValue << "-"; pNode = pNode->m_pNext; } cout << endl; }
非递归过程,稍后补上。
注意项:在链表反转后,链表尾结点需要指向nullptr
标签:test return bsp reverse 使用 rev poi color turn
原文地址:https://www.cnblogs.com/gwzz/p/9238475.html