微软面试100题之第7题

来源:互联网 发布:常见软件生命周期模型 编辑:程序博客网 时间:2024/06/07 18:24

第 7题(链表)
微软亚院之编程判断俩个链表是否相交
给出俩个单向链表的头指针,比如 h1 ,h2 ,判断这俩个链表是否相交。
为了简化问题,我们假设俩个链表均不带环。
问题扩展:
1. 如果链表可能有环列 ?
2. 如果需要求出俩个链表相交的第一节点列 ?

没有环列时
#include <stdio.h>#include <stdlib.h>typedef struct LinkList{int data;struct LinkList *next;}LinkList;LinkList *get_mutual_node(LinkList * ptr_a,LinkList * ptr_b){LinkList *b_head = ptr_b;while(ptr_a!=NULL){while(ptr_b!=NULL && ptr_b!=ptr_a){ptr_b = ptr_b->next;}if(ptr_b==ptr_a)return ptr_b;else{ptr_a = ptr_a->next;ptr_b = b_head;}}return NULL;}

有环列时
to be continued...
0 0