剑指offer-36:两个链表的第一个公共结点
题目描述
输入两个链表,找出它们的第一个公共结点。
思路
如果两个链表有共同结点,那一定是Y结构而不是X结构,以为单链表只有一个next。如下图。
找出两个链表的长度差,长的先遍历长度差的距离,然后依次对比。
代码
public class Solution36 {
public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
if(pHead1==null||pHead2==null)
return null;
int len1=getlen(pHead1);
int len2=getlen(pHead2);
if(len1>len2){
int len=len1-len2;
while(len-->0)
pHead1=pHead1.next;
}else{
int len=len2-len1;
while(len-->0)
pHead2=pHead2.next;
}
while(pHead1!=null&&pHead2!=null){
if(pHead1.val==pHead2.val)
return pHead1;
else{
pHead1=pHead1.next;
pHead2=pHead2.next;
}
}
return null;
}
public int getlen(ListNode head){
int count=0;
while(head!=null){
count++;
head=head.next;
}
return count;
}
public static void main(String[] args) {
ListNode node1=new ListNode(1);
ListNode node2=new ListNode(2);
ListNode node3=new ListNode(3);
ListNode node4=new ListNode(4);
ListNode node5=new ListNode(5);
ListNode node6=new ListNode(6);
ListNode node7=new ListNode(7);
node1.next=node2;
node2.next=node3;
node3.next=node6;
node4.next=node5;
node5.next=node6;
node6.next=node7;
ListNode node=new Solution36().FindFirstCommonNode(node1,node4);
if(node==null){
BeanUtil.print("null");
}else{
BeanUtil.print(node.val);
}
}
}
再说一个leetcode上的方法
public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
ListNode p1 = pHead1;
ListNode p2 = pHead2;
while(p1!=p2){
p1 = (p1==null ? pHead2 : p1.next);
p2 = (p2==null ? pHead1 : p2.next);
}
return p1;
}
代码分析
长度相同有公共结点,第一次就遍历到;没有公共结点,走到尾部NULL相遇,返回NULL
长度不同有公共结点,第一遍差值就出来了,第二遍一起到公共结点;没有公共,一起到结尾NULL。
最坏的情况就是没有相同点:
p1先遍历pHead1,在遍历pHead2,直到最后为null。
p2先遍历pHead2,在遍历pHead1,直到最后为null。
最后,两者为null相遇。