面接問題OJ:チェーンテーブルを反転
反転チェーンテーブルは比較的基礎的な問題であり、暴力法を直接使用すればよい.ここでは非再帰と再帰の2つのバージョンを採用している.コードを書くときは、関数の前にポインタがNULLであるかどうかをチェックしなければならない.そうしないと、空のポインタの解引用問題が発生しやすい.
ここでは、テスト例を含むコードを直接示します.
ここでは、テスト例を含むコードを直接示します.
// :
#include <iostream>
using namespace std;
struct ListNode
{
int val;
struct ListNode *next;
ListNode(int x)
:val(x), next(NULL)
{};
};
class Solution
{
public:
//
static ListNode* ReverseList(ListNode* pHead)
{
if (pHead == NULL || pHead->next == NULL)
return pHead;
ListNode* pre = NULL;
ListNode* next = NULL;
while (pHead)
{
next = pHead->next;
pHead->next = pre;
pre = pHead;
pHead = next;
}
return pre;
}
//
static ListNode* ReverListR(ListNode* pHead)
{
if (pHead == NULL || pHead->next == NULL)
return pHead;
// NULL
ListNode* newHead = ReverListR(pHead->next);
pHead->next->next = pHead; // next
pHead->next = NULL; // next NULL
return newHead;
}
static void PrintList(ListNode* pHead)
{
if (pHead == NULL)
return;
while (pHead)
{
cout << pHead->val << "->";
pHead = pHead->next;
}
cout << "NULL"<<endl;
}
};
void testNR()
{
ListNode l1(1);
ListNode l2(2);
ListNode l3(3);
ListNode l4(4);
ListNode l5(5);
ListNode l6(6);
l1.next = &l2;
l2.next = &l3;
l3.next = &l4;
l4.next = &l5;
l5.next = &l6;
Solution::PrintList(&l1);
ListNode* newHead=Solution::ReverseList(&l1);
Solution::PrintList(newHead);
}
void testR()
{
ListNode l1(1);
ListNode l2(2);
ListNode l3(3);
ListNode l4(4);
ListNode l5(5);
ListNode l6(6);
l1.next = &l2;
l2.next = &l3;
l3.next = &l4;
l4.next = &l5;
l5.next = &l6;
Solution::PrintList(&l1);
ListNode* newHead = Solution::ReverListR(&l1);
Solution::PrintList(newHead);
}
int main()
{
//testNR();
testR();
return 0;
}