找两条单链表的公共结点

来源:互联网 发布:excle怎么做数据分析 编辑:程序博客网 时间:2024/04/30 08:05

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

分析:

这是一道微软的面试题。

微软非常喜欢与链表相关的题目,

因此在微软的面试题

中,链表出现的概率相当高。

分析:这是一道微软的面试题。微软非常喜欢与链表相关的题目,因此在微软的面试题中,链表出现的概率相当高。

        如果两个单向链表有公共的结点,也就是说两个链表从某一结点开始,它们的m_pNext都指向同一个结点。但由于是单向链表的结点,每个结点只有一个m_pNext,因此从第一个公共结点开始,之后它们所有结点都是重合的,不可能再出现分叉。所以,两个有公共结点而部分重合的链表,拓扑形状看起来像一个Y,而不可能像X。

       看到这个题目,第一反应就是蛮力法:在第一链表上顺序遍历每个结点。每遍历一个结点的时候,在第二个链表上顺序遍历每个结点。如果此时两个链表上的结点是一样的,说明此时两个链表重合,于是找到了它们的公共结点。如果第一个链表的长度为m,第二个链表的长度为n,显然,该方法的时间复杂度为O(mn)。

代码如下:

#include<iostream>using namespace std;struct ListNode{int data;ListNode *next;};ListNode* Create_Linklist(int *arr,int len){ListNode* head = new ListNode();head->next = NULL;ListNode* L = head;for (int i = 0; i < len; i++){ListNode* newn = new ListNode();newn->data = arr[i];newn->next = NULL;    L->next = newn;L = L->next;}return head;}int Find_FirstofCommonNode(ListNode* &head1, ListNode* &head2){if (head1 && head1->next && head2 && head2->next){ListNode* L1;ListNode* L2;for (L1 = head1->next; L1 != NULL; L1 = L1->next){for (L2 = head2->next; L2 != NULL; L2 = L2->next){if (L1 == L2){cout << L1->data;goto Phase;}}}}Phase:return 0;}void main(int argc, char *argv[]){int arr1[] = { 1, 3, 5, 7 };ListNode* head1 = Create_Linklist(arr1, sizeof(arr1) / sizeof(int));int arr2[] = { 2, 4, 6 };ListNode* head2 = Create_Linklist(arr1, sizeof(arr1) / sizeof(int));ListNode* L2 = head2;while (L2->next)L2 = L2->next;L2->next=head1->next->next->next;Find_FirstofCommonNode(head1,head2);}

       接下来我们试着去寻找一个线性时间复杂度的算法。我们先把问题简化:如何判断两个单向链表有没有公共结点?前面已经提到,如果两个链表有一个公共结点,那么 该公共结点之后的所有结点都是重合的。那么,它们的最后一个结点必然是重合的。因此,我们判断两个链表是不是有重合的部分,只要分别遍历两个链表到最后一 个结点。如果两个尾结点是一样的,说明它们用重合;否则两个链表没有公共的结点。

       在上面的思路中,顺序遍历两个链表到尾结点的时候,我们不能保证在两个链表上同时到达尾结点。这是因为两个链表不一定长度一样。但如果假设一个链表比另一个长l个结点,我们先在长的链表上遍历l个结点,之后再同步遍历,这个时候我们就能保证同时到达最后一个结点了。由于两个链表从第一个公共结点考试到链表的尾结点,这一部分是重合的。因此,它们肯定也是同时到达第一公共结点的。于是在遍历中,第一个相同的结点就是第一个公共的结点。

      在这个思路中,我们先要分别遍历两个链表得到它们的长度,并求出两个长度之差。在长的链表上先遍历若干次之后,再同步遍历两个链表,知道找到相同的结点,或者一直到链表结束。此时,如果第一个链表的长度为m,第二个链表的长度为n,该方法的时间复杂度为O(m+n)

      基于这个思路,代码如下:

#include<iostream>using namespace std;struct ListNode{int data;ListNode *next;};ListNode* Create_Linklist(int *arr,int len){ListNode* head = new ListNode();head->next = NULL;ListNode* L = head;for (int i = 0; i < len; i++){ListNode* newn = new ListNode();newn->data = arr[i];newn->next = NULL;    L->next = newn;L = L->next;}return head;}int GetListLength(ListNode* &head){if (head == NULL||head->next==NULL)return 0;ListNode* L = head->next;int count = 0;while (L){count++;L = L->next;}return count;}ListNode* Find_FirstCommonNode(ListNode* head1, ListNode* head2){if (head1 && head1->next && head2 && head2->next){int len1 = GetListLength(head1);int len2 = GetListLength(head2);int len = len1 - len2;//若head1长度小于head2链表,则交换两个链表if (len1 < len2){ListNode* temp = head1;head1 = head2;head2 = temp;}ListNode* L1 = head1->next;ListNode* L2 = head2->next;for (int i = 0; i < abs(len); ++i){L1 = L1->next;}while (L1 && L1 != L2){L1 = L1->next;L2 = L2->next;}return  L1;}}void main(int argc, char *argv[]){int arr1[] = { 1, 3, 5, 7 };ListNode* head1 = Create_Linklist(arr1, sizeof(arr1) / sizeof(int));int arr2[] = { 2, 4, 6, 8 ,9};ListNode* head2 = Create_Linklist(arr1, sizeof(arr2) / sizeof(int));ListNode* L2 = head2;while (L2->next)L2 = L2->next;L2->next=head1->next->next->next;cout << Find_FirstCommonNode(head1, head2)<< endl;;}


0 0