《剑指Offer》面试题16:反转链表
知识点
链表和递归
题目描述
定义一个函数,输入一个链表的头结点,反转该链表并输出反转后链表的头结点。
解题思路
为了正确的反转一个链表,需要调整链表中指针的方向。为了将调整指针这个复杂的过程分析清楚,我们可以借助图形来直观的分析。在图中所示的链表中,h,i,j是3个相邻的结点。假设经过若干的操作,我们已经把结点h之前的指针调整完毕,这些结点的m_pNext都指向前面的一个结点。接下来我们把i的m_pNext指向h,此时的链表结构如图b所示。
不难注意到,由于结点i的m_pNext指向了它的前一个结点,导致我们无法在链表中遍历到结点j。为了避免链表在结点i处断开,我们需要在调整结点i的m_pNext之前,把结点j保存下来。
也就是说我们在调整结点i的m_pNext指针时,除了需要知道结点i本身之外,还需要i的前一个结点h,因为我们需要把结点i的m_pNext指向结点h,同时,我们还实现需要保存i个结点j,以防止链表断开。因此相应地我们需要三个指针,分别指向当前遍历到的结点,它的前一个结点和后一个结点。
最后我们试着找到反转链表的头结点。不难分析出反转后的链表的头结点是原始链表的尾节点。什么结点是尾节点,自然是m_pNext为 Null 的结点。
测试用例
代码(原书)
// 《剑指Offer——名企面试官精讲典型编程题》代码
// 著作权所有者:何海涛
#include "stdio.h"
#include "cstdlib"
struct ListNode
{
int m_nValue;
ListNode *m_pNext;
};
ListNode* CreateListNode(int value)
{
ListNode* pNode = new ListNode();
pNode->m_nValue = value;
pNode->m_pNext = NULL;
return pNode;
}
void ConnectListNodes(ListNode* pCurrent, ListNode* pNext)
{
if (pCurrent == NULL)
{
printf("Error to connect two nodes.\n");
exit(1);
}
pCurrent->m_pNext = pNext;
}
void PrintListNode(ListNode* pNode)
{
if (pNode == NULL)
{
printf("The node is NULL\n");
}
else
{
printf("The key in node is %d.\n", pNode->m_nValue);
}
}
void PrintList(ListNode* pHead)
{
printf("PrintList starts.\n");
ListNode* pNode = pHead;
while (pNode != NULL)
{
printf("%d\t", pNode->m_nValue);
pNode = pNode->m_pNext;
}
printf("\nPrintList ends.\n");
}
void DestroyList(ListNode* pHead)
{
ListNode* pNode = pHead;
while (pNode != NULL)
{
pHead = pHead->m_pNext;
delete pNode;
pNode = pHead;
}
}
//=============非递归实现链表反转===========================
ListNode* ReverseList(ListNode* pHead)
{
ListNode* pReversedHead = NULL;
ListNode* pNode = pHead;
ListNode* pPrev = NULL;
while (pNode != NULL)
{
ListNode* pNext = pNode->m_pNext;
if (pNext == NULL)
pReversedHead = pNode;
pNode->m_pNext = pPrev;
pPrev = pNode;
pNode = pNext;
}
return pReversedHead;
}
//=============递归实现链表反转===========================
ListNode* ReverseListRecursive(ListNode *pHead) //递归算法
{
if (pHead == NULL)
return NULL;
ListNode *pNode, *reverse_head, *pNext;
if (pHead->m_pNext == NULL) // 链表中只有一个结点,逆转后的头指针不变
return pHead;
else
{
pNode = pHead;
pNext = pHead->m_pNext; // pNext为(a2,...an)的头指针
reverse_head = ReverseListRecursive(pNext); // 逆转链表(a2,...an),并返回逆转后的头指针
pNext->m_pNext = pNode; // 将a1链接在a2之后
pNode->m_pNext = NULL;
}
return reverse_head; // (a2,...an)逆转链表的头指针即为(a1,a2,...an)逆转链表的头指针
}
// ====================测试代码====================
ListNode* Test(ListNode* pHead)
{
printf("The original list is: \n");
PrintList(pHead);
ListNode* pReversedHead = ReverseList(pHead);
//ListNode* pReversedHead = ReverseListRecursive(pHead);
printf("The reversed list is: \n");
PrintList(pReversedHead);
return pReversedHead;
}
// 输入的链表有多个结点
void Test1()
{
ListNode* pNode1 = CreateListNode(1);
ListNode* pNode2 = CreateListNode(2);
ListNode* pNode3 = CreateListNode(3);
ListNode* pNode4 = CreateListNode(4);
ListNode* pNode5 = CreateListNode(5);
ConnectListNodes(pNode1, pNode2);
ConnectListNodes(pNode2, pNode3);
ConnectListNodes(pNode3, pNode4);
ConnectListNodes(pNode4, pNode5);
ListNode* pReversedHead = Test(pNode1);
DestroyList(pReversedHead);
}
// 输入的链表只有一个结点
void Test2()
{
ListNode* pNode1 = CreateListNode(1);
ListNode* pReversedHead = Test(pNode1);
DestroyList(pReversedHead);
}
// 输入空链表
void Test3()
{
Test(NULL);
}
int main()
{
Test1();
Test2();
Test3();
system("pause");
return 0;
}
代码(牛客网)
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
//法1:非递归
/*ListNode* ReverseList(ListNode* pHead) {
if(pHead == NULL)
return NULL;
if(pHead->next == NULL)
return pHead;
ListNode *pNode = pHead;
ListNode *pPrev = NULL;
ListNode *reverseHead = NULL;
while(pNode != NULL)
{
ListNode *pNext = pNode->next;
pNode->next = pPrev;
if(pNext== NULL)
reverseHead = pNode;
pPrev = pNode;
pNode = pNext;
}
return reverseHead;
}*/
//法2:递归
ListNode* ReverseList(ListNode* pHead) {
if(pHead == NULL)
return NULL;
if(pHead->next == NULL)
return pHead;
ListNode *pNode = pHead;
ListNode *pNext = pNode->next;
ListNode *reverseHead = ReverseList(pNext);
pNext->next = pNode;
pNode->next = NULL;
return reverseHead;
}
};