#include <iostream>
using namespace std;
//结点定义
struct ListNode
{
int m_nValue;
ListNode *m_pNext;
};
//创建结点
ListNode *CreateListNode(int m)
{
ListNode *pNode = new ListNode;
pNode -> m_nValue = m;
pNode -> m_pNext = NULL;
return pNode;
}
//连接结点
void ConnectListNode(ListNode *pNode1, ListNode *pNode2)
{
pNode1 -> m_pNext = pNode2;
}
//释放空间(递归实现)
void FreeSpace(ListNode *pHead)
{
if(pHead)
{
FreeSpace(pHead -> m_pNext);
delete pHead;
}
}
//删除结点
void DestroyListNode(ListNode *pNode)
{
delete pNode;
}
//计算链表长度
int GetListLength(ListNode *pHead)
{
int Length = 0;
while(pHead)
{
Length++;
pHead = pHead -> m_pNext;
}
return Length;
}
//寻找两链表的公共结点
ListNode *GetCommonNode(ListNode *pHead1, ListNode *pHead2)
{
//初始化链表各变量(假设链表1的长度比链表2的长)
int Length1 = GetListLength(pHead1);
int Length2 = GetListLength(pHead2);
int LengthDis = Length1 - Length2;
ListNode *pListLong = pHead1;
ListNode *pListShort = pHead2;
//链表2比链表1长的情况
if(Length1 < Length2)
{
LengthDis = Length2 - Length1;
pListLong = pHead2;
pListShort = pHead1;
}
//长链表先走进不
for(int i = 0; i < LengthDis; ++i)
{
pListLong = pListLong -> m_pNext;
}
//长度相同时,同时遍历两链表寻找相同结点
while(pListLong != NULL && pListShort != NULL && pListLong != pListShort)
{
pListLong = pListLong -> m_pNext;
pListShort = pListShort -> m_pNext;
}
ListNode *pCommonNode = pListLong;
return pCommonNode;
}
//============================测试代码===================
void Test(char *TestName, ListNode *pHead1, ListNode *pHead2, ListNode *pExpected)
{
if(TestName != NULL)
cout << TestName << "begins: " << endl;
ListNode *pResult = GetCommonNode(pHead1, pHead2);
if(pResult == pExpected)
cout << "Passed!" << endl;
else
cout << "Failed!" << endl;
}
//==========================测试用例=====================
//1 -> 2 -> 3 \
// 6 -> 7
// 4 -> 5 /
//第一个公共结点在链表中间
void Test1()
{
ListNode *pNode1 = CreateListNode(1);
ListNode *pNode2 = CreateListNode(2);
ListNode *pNode3 = CreateListNode(3);
ListNode *pNode4 = CreateListNode(4);
ListNode *pNode5 = CreateListNode(5);
ListNode *pNode6 = CreateListNode(6);
ListNode *pNode7 = CreateListNode(7);
ConnectListNode(pNode1, pNode2);
ConnectListNode(pNode2, pNode3);
ConnectListNode(pNode3, pNode6);
ConnectListNode(pNode6, pNode7);
ConnectListNode(pNode4, pNode5);
ConnectListNode(pNode5, pNode6);
ConnectListNode(pNode6, pNode7);
Test("Test1", pNode1, pNode4, pNode6);
FreeSpace(pNode1);
DestroyListNode(pNode4);
DestroyListNode(pNode5);
}
//没有公共结点
//1 -> 2 -> 3 -> 4
//5 -> 6 -> 7
void Test2()
{
ListNode *pNode1 = CreateListNode(1);
ListNode *pNode2 = CreateListNode(2);
ListNode *pNode3 = CreateListNode(3);
ListNode *pNode4 = CreateListNode(4);
ListNode *pNode5 = CreateListNode(5);
ListNode *pNode6 = CreateListNode(6);
ListNode *pNode7 = CreateListNode(7);
ConnectListNode(pNode1, pNode2);
ConnectListNode(pNode2, pNode3);
ConnectListNode(pNode3, pNode4);
ConnectListNode(pNode5, pNode6);
ConnectListNode(pNode6, pNode7);
Test("Test2", pNode1, pNode5, NULL);
FreeSpace(pNode1);
FreeSpace(pNode5);
}
//最后一个结点是公共结点
// 1 -> 2 -> 3 -> 4 \
// 7
// 5 -> 6 /
void Test3()
{
ListNode *pNode1 = CreateListNode(1);
ListNode *pNode2 = CreateListNode(2);
ListNode *pNode3 = CreateListNode(3);
ListNode *pNode4 = CreateListNode(4);
ListNode *pNode5 = CreateListNode(5);
ListNode *pNode6 = CreateListNode(6);
ListNode *pNode7 = CreateListNode(7);
ConnectListNode(pNode1, pNode2);
ConnectListNode(pNode2, pNode3);
ConnectListNode(pNode3, pNode4);
ConnectListNode(pNode4, pNode7);
ConnectListNode(pNode5, pNode6);
ConnectListNode(pNode6, pNode7);
Test("Test3", pNode1, pNode5, pNode7);
FreeSpace(pNode1);
DestroyListNode(pNode5);
DestroyListNode(pNode6);
}
//公共结点是第一个结点
//1 -> 2 -> 3 -> 4
//两个链表完全重合
void Test4()
{
ListNode *pNode1 = CreateListNode(1);
ListNode *pNode2 = CreateListNode(2);
ListNode *pNode3 = CreateListNode(3);
ListNode *pNode4 = CreateListNode(4);
ConnectListNode(pNode1, pNode2);
ConnectListNode(pNode2, pNode3);
ConnectListNode(pNode3, pNode4);
Test("Test4", pNode1, pNode1, pNode1);
FreeSpace(pNode1);
}
//输入的链表中有一个空链表
//1 -> 2 -> 3 -> 4 -> 5
void Test5()
{
ListNode *pNode1 = CreateListNode(1);
ListNode *pNode2 = CreateListNode(2);
ListNode *pNode3 = CreateListNode(3);
ListNode *pNode4 = CreateListNode(4);
ListNode *pNode5 = CreateListNode(5);
ConnectListNode(pNode1, pNode2);
ConnectListNode(pNode2, pNode3);
ConnectListNode(pNode3, pNode4);
ConnectListNode(pNode4, pNode5);
Test("Test5", pNode1, NULL, NULL);
FreeSpace(pNode1);
}
//输入两个空链表
void Test6()
{
Test("Test6", NULL, NULL, NULL);
}
int main()
{
Test1();
Test2();
Test3();
Test4();
Test5();
Test6();
return 0;
}
面试题37:两个链表的第一个公共结点
最新推荐文章于 2024-07-05 16:24:46 发布