@snuffles
2019-04-10T09:50:58.000000Z
字数 311
阅读 1108
链表
编写一个程序,找到两个单链表相交的起始节点。
解:分别遍历两个链表,得到分别对应的长度,然后求长度的差值,把比较长的那个链表向后移动这个差值的个数,然后一一比较。
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {if (!headA) return NULL;if (!headB) return NULL;ListNode * p = headA;ListNode * q = headB;for(;p!=NULL;p=p->next){for(q=headB;q!=NULL;q=q->next){if (p == q) return p;}}return NULL;}
