题目:
输入两个链表,找出它们的第一个公共结点。
解题:
/* 链表A:1-2-3-4-5 链表B:a-b-4-5 则混合链表1:1-2-3-4-5-null-a-b-4-5 混合链表2:a-b-4-5-null-1-2-3-4-5 则可以找到第一个公共结点:4 */ public class Solution { public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) { if(pHead1 == null || pHead2 == null) return null; ListNode p1 = pHead1; ListNode p2 = pHead2; while(p1 != p2) { if(p1 == null) p1 = pHead2; else p1 = p1.next; if(p2 == null) p2 = pHead1; else p2 = p2.next; } return p1; } }