zoukankan      html  css  js  c++  java
  • 找出链表的第一个公共结点

    题目:两个单链表,找出它们的第一个公共结点。

    链表的结点定义为:

    struct ListNode
    {
        int m_nKey;
        ListNode* m_pNext;
    };

    答:

    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    struct ListNode
    {
        int m_nKey;
        ListNode* m_pNext;
    };
    
    //构造链表
    void CreateList(ListNode *&pHead, fstream &fin, ListNode *pNodeCommon)
    {
        ListNode *pNode = NULL;
        ListNode *pTmp = NULL;
        int data;
        fin>>data;
        while (data)
        {
            pNode = new ListNode;
            pNode->m_nKey = data;
            pNode->m_pNext = NULL;
            if (NULL == pHead)
            {
                pHead = pNode;
                pTmp = pNode;
            }
            else
            {
                pTmp->m_pNext = pNode;
                pTmp = pNode;
            }
    
            fin>>data;
        }
        if (NULL != pNode)
        {
            pNode->m_pNext = pNodeCommon;
        }
        
    }
    
    //求链表长度
    int ListLength(ListNode *pHead)
    {
        if (NULL == pHead)
        {
            return 0;
        }
        ListNode *pNode = pHead;
        int length = 0;
        while (NULL!= pNode)
        {
            length++;
            pNode = pNode->m_pNext;
        }
        return length;
    }
    
    //找出链表的第一个公共结点
    ListNode* FindFirstCommonNode(ListNode *pFirstHead, ListNode *pSecondHead)
    {
        ListNode *pShortNode = pFirstHead;
        ListNode *pLongNode = pSecondHead;
        int lengthShort = ListLength(pShortNode);
        int lengthLong = ListLength(pLongNode);
        
        if (lengthShort > lengthLong)   //1、求出两个链表的长度
        {
            int temp = lengthLong;
            lengthLong = lengthShort;
            lengthShort = temp;
            ListNode *PTmp = pShortNode;
            pShortNode = pLongNode;
            pLongNode = PTmp;
        }
        int separate = lengthLong - lengthShort;  
        
        for (int i = 0; i < separate && NULL != pLongNode; i++)  //2、让长度长的链表先遍历两个链表的长度的差值
        {
            pLongNode = pLongNode->m_pNext;
        }
    
        while (NULL != pShortNode && NULL != pLongNode && (pShortNode != pLongNode))  //3、第一个相等的链表结点就是第一个公共结点
        {
            pShortNode = pShortNode->m_pNext;
            pLongNode = pLongNode->m_pNext;
        }
        if (pShortNode == pLongNode)
        {
            return pShortNode;
        }
        return NULL;
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        fstream finShort("listShort.txt");
        fstream finLong("listLong.txt");
        fstream finCommon("listCommon.txt");
        ListNode *pNodeShort = NULL;
        ListNode *pNodeLong = NULL;
        ListNode *pNodeCommon = NULL;
        CreateList(pNodeCommon, finCommon, NULL);
        CreateList(pNodeShort, finShort, pNodeCommon);
        CreateList(pNodeLong, finLong, pNodeCommon);
        ListNode *pFirstCommon = FindFirstCommonNode(pNodeShort, pNodeLong);
        if (NULL != pFirstCommon)
        {
            cout<<"两个链表的第一个公共结点为:"<<pFirstCommon->m_nKey;
        }
        else
        {
            cout<<"两个链表没有公共结点";
        }
        cout<<endl;
        return 0;
    }

    运行界面如下:

    建造长链表用到的listLong.txt为:

    555 8 9 20 44 33 20 8 4 0

    建造短链表用到的listShort.txt为:

    7 16 39 0

    建造公共链表用到的listCommon.txt为:

    555 4 50 77 99 0
  • 相关阅读:
    C# listbox鼠标选择改变改行颜色的另一种方便方法
    非专业码农 JAVA学习笔记 4 java继承和多态
    转:Java学习笔记之方法重载,动态方法调度和抽象类
    非专业码农 JAVA学习笔记 3 抽象、封装和类(2)
    使用bootstrap简单制作Tab切换页
    转载:CSS从大图中抠取小图完整教程(background-position应用)
    xhEditor 整理用法
    SCADA开源项目lite版本
    ImageSharp源码详解之JPEG压缩原理(3)DCT变换
    ImageSharp源码详解之JPEG压缩原理(4)熵编码
  • 原文地址:https://www.cnblogs.com/venow/p/2658157.html
Copyright © 2011-2022 走看看