输入两个链表,找出它们的第一个公共结点。
输入可能包含多个测试样例。
对于每个测试案例,输入的第一行为两个整数m和n(1<=m,n<=1000):代表将要输入的两个链表的元素的个数。
接下来的两行,第一行为第一个链表的所有元素,中间用空格隔开。第二行为第二个链表的所有元素,中间用空格隔开。
对应每个测试案例,
输出两个链表的第一个公共结点的值。
如果两个链表没有公共结点,则输出“My God”。
5 4 1 2 3 6 7 4 5 6 7 3 3 1 5 7 2 4 7 2 3 1 3 4 5 6
6 7 My God
分析:
以下代码在eclipse cdt下运行正常,但在gcc下回报segment fault,公司gentoo下gcc有问题,回家再调试~
(顺便鄙视下用数组做的人。。。)
#include<stdio.h> #include<stdlib.h> typedef struct node { int data; struct node *next; } linklist; linklist *reverseList(linklist *head) { if(head == NULL) { return NULL; } else { linklist *p, *tmp, *pNext; tmp = NULL; p = head->next; while(p) { pNext = p->next; if(p->next == NULL) { head = p; } p->next = tmp; tmp = p; p = pNext; } return head; } } /*free a linklist*/ void freeList(linklist *head) { linklist *p = head; if(p->next) { freeList(p->next); } free(p); p = NULL; } void debug(linklist *head) { linklist *p = head; while(p) { printf("%d ", p->data); p = p->next; } printf("\n"); } int main() { int m, n, i, v; linklist *L1, *L2, *tail1, *tail2, *p; while (scanf("%d %d", &m, &n) != EOF) { L1 = (linklist *)malloc(sizeof(linklist)); if(!L1) exit(0); L2 = (linklist *)malloc(sizeof(linklist)); if(!L2) exit(0); L1->next = NULL; L2->next = NULL; tail1 = L1; tail2 = L2; for (i = 0; i < m; i++) { scanf("%d", &v); p = (linklist *) malloc(sizeof(linklist)); //printf("i:%d, p1:%d\n", i, p); if(!p) exit(0); p->data = v; p->next = NULL; tail1->next = p; tail1 = p; } for (i = 0; i < n; i++) { scanf("%d", &v); p = (linklist *) malloc(sizeof(linklist)); /* if(!p) { printf("error\n");return 1; }*/ if(!p) exit(0); //printf("i:%d, p2:%d\n", i, p); p->data = v; p->next = NULL; tail2->next = p; tail2 = p; } L1 = reverseList(L1); L2 = reverseList(L2); /*注意此时,L2 == p*/ //debug(L1); //debug(L2); linklist *p1 = L1; linklist *p2 = L2; if(p1->data != p2->data) { printf("My God\n"); } else { linklist *pre; while(p1 && p2 && p1->data == p2->data) { pre = p1;/*存下来*/ p1 = p1->next; p2 = p2->next; } printf("%d\n", pre->data); } freeList(L1); freeList(L2); } return 0; }
---------------------------------------------------------------------------------
update:
感谢@buptpatriot的分析,通过gdb工具找到了出错的代码段,错误原因是对链表的内存free不对。
通过L2 = reverseList(L2)后, L2 == p了,所以如果free(p),再free(L2)就是对野指针free,这是错误的。而且只free(L1)也是不对的,所以重新写了一个freeList()函数(见下面的代码:)
#include<stdio.h> #include<stdlib.h> typedef struct node { int data; struct node *next; } linklist; linklist *reverseList(linklist *head) { if(head == NULL) { return NULL; } else { linklist *p, *tmp, *pNext; tmp = NULL; p = head->next; while(p) { pNext = p->next; if(p->next == NULL) { head = p; } p->next = tmp; tmp = p; p = pNext; } return head; } } /*free a linklist*/ void freeList(linklist *head) { linklist *p = head; if(p->next) { freeList(p->next); } free(p); p = NULL; } void debug(linklist *head) { linklist *p = head; while(p) { printf("%d ", p->data); p = p->next; } printf("\n"); } int main() { int m, n, i, v; linklist *L1, *L2, *tail1, *tail2, *p; while (scanf("%d %d", &m, &n) != EOF) { L1 = (linklist *)malloc(sizeof(linklist)); if(!L1) exit(0); L2 = (linklist *)malloc(sizeof(linklist)); if(!L2) exit(0); L1->next = NULL; L2->next = NULL; tail1 = L1; tail2 = L2; for (i = 0; i < m; i++) { scanf("%d", &v); p = (linklist *) malloc(sizeof(linklist)); //printf("i:%d, p1:%d\n", i, p); if(!p) exit(0); p->data = v; p->next = NULL; tail1->next = p; tail1 = p; } for (i = 0; i < n; i++) { scanf("%d", &v); p = (linklist *) malloc(sizeof(linklist)); /* if(!p) { printf("error\n");return 1; }*/ if(!p) exit(0); //printf("i:%d, p2:%d\n", i, p); p->data = v; p->next = NULL; tail2->next = p; tail2 = p; } L1 = reverseList(L1); L2 = reverseList(L2); /*注意此时,L2 == p*/ //debug(L1); //debug(L2); linklist *p1 = L1; linklist *p2 = L2; if(p1->data != p2->data) { printf("My God\n"); } else { linklist *pre; while(p1 && p2 && p1->data == p2->data) { pre = p1;/*存下来*/ p1 = p1->next; p2 = p2->next; } printf("%d\n", pre->data); } freeList(L1); freeList(L2); } return 0; }
上面的代码在九度并没有AC,我和@buptpatriot讨论后,认为它的题目是bull shit.
这是一个AC的代码,大家可以看看 :
#include <stdio.h> #define MAXN 10000 int arr1[MAXN],arr2[MAXN]; int main() { int m,n; while(scanf("%d %d",&m,&n)!=EOF){ for(int i=0;i<m;i++) scanf("%d",&arr1[i]); for(int i=0;i<n;i++) scanf("%d",&arr2[i]); if(m>n){ int i=0; while(arr1[m-n+i]!=arr2[i]) i++; if(i<n) printf("%d\n",arr2[i]); else printf("My God\n"); } else{ int i=0; while(arr2[n-m+i]!=arr1[i]) i++; if(i<m) printf("%d\n",arr1[i]); else printf("My God\n"); } } return 0; } /************************************************************** Problem: 1505 Language: C Result: Accepted Time:70 ms Memory:1100 kb ****************************************************************/
我的AC代码(链表实现):
#include<stdio.h> #include<stdlib.h> typedef struct node { int data; struct node *next; } linklist; /*free a linklist*/ void freeList(linklist *head) { linklist *p = head; if(p->next) { freeList(p->next); } free(p); p = NULL; } int main() { int m, n, i, v; linklist *L1, *L2, *tail1, *tail2, *p; while (scanf("%d %d", &m, &n) != EOF) { L1 = (linklist *)malloc(sizeof(linklist)); if(!L1) exit(0); L2 = (linklist *)malloc(sizeof(linklist)); if(!L2) exit(0); L1->next = NULL; L2->next = NULL; tail1 = L1; tail2 = L2; for (i = 0; i < m; i++) { scanf("%d", &v); p = (linklist *) malloc(sizeof(linklist)); if(!p) exit(0); p->data = v; p->next = NULL; tail1->next = p; tail1 = p; } for (i = 0; i < n; i++) { scanf("%d", &v); p = (linklist *) malloc(sizeof(linklist)); if(!p) exit(0); p->data = v; p->next = NULL; tail2->next = p; tail2 = p; } linklist *p1 = L1->next; linklist *p2 = L2->next; if(m > n) { for(i = 0; i < m-n; i++) { // printf("p1:%d, p2:%d\n", p1->data, p2->data); p1 = p1->next; } while(p1 && p2 && p1->data != p2->data) { // printf("p1:%d, p2:%d\n", p1->data, p2->data); p1 = p1->next; p2 = p2->next; } if(p1) { printf("%d\n", p1->data); } else { printf("My God\n"); } } else { for(i = 0; i < n-m; i++) { p2 = p2->next; } while(p1 && p2 && p1->data != p2->data) { p1 = p1->next; p2 = p2->next; } if(p2) { printf("%d\n", p2->data); } else { printf("My God\n"); } } freeList(L1); freeList(L2); } return 0; } /************************************************************** Problem: 1505 User: wusuopuBUPT Language: C Result: Accepted Time:70 ms Memory:912 kb ****************************************************************/
------------------------------------------------
再update:
链表题目汇总