判断2个单链表是否相交,并求出第一个相交结点

不考虑单链表有环的情况下

如果2个单链表相交,一定是Y型链表

1.遍历2个链表到尾结点,记录2个链表的长度x,y
2.尾结点相同,则相交。
3.从表头开始,长链表先走|x-y|步,之后2个链表一起走,判断第一个相同的点。
 
  1 #include <stdio.h>  
  2 #include <stdlib.h>  
  3 #include <string.h>  
  4 typedef struct student             //定义链表结构  
  5 {  
  6     int num;  
  7     struct student *pnext;  
  8 }stu,*pstu;  
  9 void link_tail_insert(pstu ,pstu ,int );                        //尾插法建立2个链表  
 10 void link_show(pstu );                                                       //展示链表  
 11 void link_judge_intersect(pstu ,pstu );                          //判断链表是否有交点  
 12 void link_bulid_intersect(pstu ,pstu ,pstu ,pstu *); //建立Y型链表  
 13 void main(){  
 14     pstu phead1,ptail1,phead2,ptail2;  
 15     int i;  
 16     phead1 = NULL;  
 17     ptail1 = NULL;  
 18     phead2 = NULL;  
 19     ptail2 = NULL;  
 20     while(scanf("%d",&i) != EOF){                                       
 21         link_tail_insert(&phead1,&ptail1,i);  
 22         link_tail_insert(&phead2,&ptail2,i);  
 23     }  
 24     link_show(phead1);  
 25     link_bulid_intersect(phead1,phead2,ptail1,&ptail2);  
 26     link_show(phead2);    
 27     link_judge_intersect(phead1,phead2);  
 28     system("pause");  
 29   
 30 }  
 31 void link_tail_insert(pstu *phead,pstu *ptail,int i){              //尾插法建立链表  
 32     pstu pnew;  
 33     pnew = (pstu)malloc(sizeof(stu));  
 34     memset(pnew,0,sizeof(stu));  
 35     pnew->num = i;  
 36     if(*ptail == NULL){  
 37         *phead = pnew;  
 38         *ptail = pnew;  
 39     }  
 40     else{  
 41         (*ptail)->pnext = pnew;  
 42         *ptail = pnew;  
 43     }  
 44 }  
 45 void link_show(pstu phead){                                         //输出链表  
 46     pstu pshow;  
 47     pshow = phead;  
 48     if(phead == NULL)  
 49     {  
 50         printf("no exsit\n");  
 51         return;  
 52     }  
 53     while(pshow != NULL){  
 54         printf("%d ",pshow->num);  
 55         pshow = pshow->pnext;  
 56     }  
 57     putchar('\n');  
 58 }  
 59 void link_bulid_intersect(pstu phead1,pstu phead2,pstu ptail1,pstu *ptail2){   //把第2个链表拼接到第一个链表第三个结点的位置。  
 60     pstu i;  
 61     i = phead1;  
 62     i = i->pnext->pnext;            
 63     (*ptail2)->pnext = i;  
 64     *ptail2 = ptail1;  
 65 }  
 66 void link_judge_intersect(pstu phead1,pstu phead2){             //判断链表是否有交点  
 67     int x,y;  
 68     pstu i,j,prei,prej;  
 69     x = y = 0;  
 70     i = prei = phead1;                                        //建立前置结点,和当前结点  
 71     j = prej = phead2;  
 72     while(i != NULL){                                        //2个链表运行到尾部  
 73         prei = i;  
 74         i = i->pnext;  
 75         x++;  
 76     }  
 77     while(j != NULL){  
 78         prej = j;  
 79         j = j->pnext;  
 80         y++;  
 81     }  
 82     if(prei != prej)                                      //尾结点不相同 没有相交  
 83         printf("Link has no intersect.\n");  
 84     else{  
 85         i = phead1;  
 86         j = phead2;  
 87         if(x > y){                                        //第1个链表长度长,先走X-Y步  
 88             x = x - y;  
 89             while(x > 0){  
 90                 i = i->pnext;  
 91                 x--;  
 92             }  
 93             while(i != NULL){                              //一起走,知道相遇或者达到NULL  
 94                 if(i == j){  
 95                     printf("Link has intersect.\n");  
 96                     printf("%d\n",i->num);  
 97                     break;  
 98                 }  
 99                 i = i->pnext;  
100                 j = j->pnext;  
101             }  
102         }  
103         else{  
104             y = y - x;                           //第2个链表长度长,先走X-Y步  
105             while(y > 0){  
106                 j = j->pnext;  
107                 y--;  
108             }  
109             while(i != NULL){                       //一起走,知道相遇或者达到NULL  
110                 if(i == j){  
111                     printf("Link has intersect.\n");  
112                     printf("%d\n",i->num);  
113                     break;  
114                 }  
115                 i = i->pnext;  
116                 j = j->pnext;  
117             }  
118         }  
119           
120     }  
121 }  

 

转载于:https://www.cnblogs.com/boluo007/p/5470228.html

在C++中,找到两个单链表的公共节点可以使用双指针法。这个方法的基本思想是,分别从两个链表的头部开始,同时前进,如果它们指向同的节点,则说明找到了公共节点。当其中一个链表遍历完时,另一个链表应该还在继续遍历,直到遇到同的节点或者到达链表尾部。 下面是一个简单的C++函数,它接收两个链表的头节点作为输入,返回第一个公共节点,如果没有公共节点则返回nullptr: ```cpp #include <iostream> using namespace std; // 链表节点结构 struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; ListNode* findIntersectionNode(ListNode* list1, ListNode* list2) { if (!list1 || !list2) return nullptr; // 如果任何一个链表为空,直接返回null // 让快指针先走一步 ListNode* fast = list1; ListNode* slow = list1; // 当快慢指针都指向链表尾部时,它们相交 while (fast && fast->next) { fast = fast->next->next; slow = slow->next; if (fast == slow) break; } // 如果没相交,直接返回 if (!fast || !fast->next) return nullptr; // 从链表头部开始,让两个指针再次遇 fast = list1; slow = slow->next; while (fast != slow) { fast = fast->next; slow = slow->next; } return fast; // 返回公共节点 } int main() { // 测试示例 ListNode* list1 = new ListNode(1); list1->next = new ListNode(2); list1->next->next = new ListNode(3); list1->next->next->next = new ListNode(4); list1->next->next->next->next = new ListNode(5); ListNode* list2 = new ListNode(6); list2->next = new ListNode(7); list2->next->next = new ListNode(8); list2->next->next->next = list1->next->next; // 列表2的部分链路列表1共享 ListNode* intersection = findIntersectionNode(list1, list2); if (intersection) cout << "Intersection node found: " << intersection->val << endl; else cout << "No intersection found." << endl; return 0; } ``` 在这个示例中,我们创建了两个链表,其中`list2`的一部分`list1`有共同节点。`findIntersectionNode`函数会找到这两个链表第一个公共节点。如果没有公共节点,函数将返回nullptr。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值