37. 兩個鏈表的第一個公共結點

  題目:輸入兩個鏈表,找出它們的第一個公共結點。node

  思路:先遍歷兩個鏈表得出它們各自的長度,而後讓長鏈表先走,直到長度和短的一致,而後兩個鏈表一塊兒走。ios

  相關題目:
  若是把圖5.3(兩個鏈表重複結點的Y型圖)逆時針旋轉90度,咱們就會發現兩個鏈表的拓撲形狀和一棵樹的形狀很是類似,只是這裏的指針式從葉結點指向根結點的。兩個鏈表的第一個公共結點正好是二叉樹中兩個葉節點的最低公共祖先。spa

#include<iostream>
#include<cstdio>
using namespace std;

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)
    {
        cout << "Error to connect two nodes." << endl;
        return;
    }

    pCurrent->m_pNext = pNext;
}

//打印鏈表
void PrintListNode(ListNode* pHead)
{

    ListNode* pNode = pHead;
    while (pNode != NULL)
    {
        cout << pNode->m_nValue << "  ";
        pNode = pNode->m_pNext;
    }

    cout << endl << "PrintList end." << endl;
}

//銷燬鏈表節點
void DestroyList(ListNode* pHead)
{
    ListNode* pNode = pHead;
    while (pNode != NULL)
    {
        pHead = pHead->m_pNext;
        delete pNode;
        pNode = pHead;
    }
}


unsigned int GetListLength(ListNode* pHead)
{
    unsigned int length = 0;
    ListNode* pNode = pHead;
    while (pNode != NULL)
    {
        ++length;
        pNode = pNode->m_pNext;
    }

    return length;
}

ListNode* FindFirstCommomNode(ListNode* pHead1, ListNode* pHead2)
{
    //獲得兩個鏈表的長度
    unsigned int length1 = GetListLength(pHead1);
    unsigned int length2 = GetListLength(pHead2);
    int lengthdiff = length1 - length2;             //判斷哪一個長哪一個短

    ListNode* longList = pHead1;
    ListNode* shortList = pHead2;
    if (length2 > length1)
    {
        longList = pHead2;
        shortList = pHead1;
        lengthdiff = length2 - length1;
    }

    //先在長鏈表上走幾步,再同時在兩個鏈表上遍歷
    for (int i = 0; i < lengthdiff; i++)
    {
        longList = longList->m_pNext;
    }

    while ((longList != NULL) && (shortList != NULL) && (longList != shortList))
    {
        longList = longList->m_pNext;
        shortList = shortList->m_pNext;
    }

    //獲得第一個公共結點
    ListNode* firstCommonNode = longList;

    return firstCommonNode;
}

void test1()
{
    ListNode* node1 = CreateListNode(1);
    ListNode* node2 = CreateListNode(2);
    ListNode* node3 = CreateListNode(3);
    ListNode* node4 = CreateListNode(4);
    ListNode* node5 = CreateListNode(5);
    ListNode* node6 = CreateListNode(6);
    ListNode* node7 = CreateListNode(7);

    ConnectListNodes(node1, node2);
    ConnectListNodes(node2, node3);
    ConnectListNodes(node3, node6);
    ConnectListNodes(node6, node7);

    PrintListNode(node1);

    ConnectListNodes(node4, node5);
    ConnectListNodes(node5, node6);
    ConnectListNodes(node6, node7);

    PrintListNode(node4);

    ListNode* node = FindFirstCommomNode(node1, node4);
    cout << node->m_nValue << endl;

    //DestroyList(node1);
    //DestroyList(node4);  //這邊銷燬得用DestroyNode,一個一個的銷燬
}

int main()
{
    test1();

    return 0;
}
相關文章
相關標籤/搜索