两个链表的第一个公共结点

来源:互联网 发布:平板一键刷机软件 编辑:程序博客网 时间:2024/06/05 15:38
题目描述

输入两个链表,找出它们的第一个公共结点。

struct ListNode {int val;struct ListNode *next;ListNode(int x) :val(x), next(NULL) {};class Solution{public:ListNode* FindFirstCommonNode(ListNode *pHead1,ListNode *pHead2){int length1 = GetListLength(pHead1);int length2 = GetListLength(pHead2);int lengthDif =0;ListNode *pListHeadLong =NULL;ListNode *pListHeadShort =NULL;//得到两个链表的长度差,并用连个结点标记长短链表if(length1>length2){pListHeadLong = pHead1;pListHeadShort =pHead2;lengthDif = length1-length2;}else{pListHeadLong = pHead2;pListHeadShort = pHead1;lengthDif = length2-length1;}//较长的链表先走几步for(int i=0;i<lengthDif;i++)pListHeadLong = pListHeadLong->next;//此时两个链表同时遍历while((pListHeadLong!=NULL)&&(pListHeadShort!=NULL)&&(pListHeadShort!=pListHeadLong)){pListHeadShort=pListHeadShort->next;pListHeadLong = pListHeadLong->next;}return pListHeadShort;}int GetListLength(ListNode *pHead){int length =0;ListNode *pNode =pHead;while(pNode!=NULL){pNode= pNode->next;length++;}return length;}};

方法2:

class Solution{public:ListNode *FindFirstCommonNode(ListNode *pHead1,ListNode *pHead2){map<ListNode*,int > M;ListNode *pNode = pHead1;while(pNode!=NULL){M[pNode] =1;pNode = pNode->next;}pNode = pHead2;while(pNode!=NULL){if(M[pNode]){return pNode;}pNode = pNode->next;}return NULL;}};


0 0
原创粉丝点击