剑指offer编程题(JAVA实现)——第36题:两个链表的第一和公共节点



githubhttps://github.com/JasonZhangCauc/JZOffer
  • 剑指offer编程题(JAVA实现)——第36题:两个链表的第一和公共节点
  • 输入两个链表,找出它们的第一个公共结点。
public class Test36 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}
	
	public class ListNode {
	    int val;
	    ListNode next = null;

	    ListNode(int val) {
	        this.val = val;
	    }
	}
	
	public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
		ListNode listNode1 = pHead1;
		ListNode listNode2 = pHead2;
		while (listNode1 != listNode2) {
			listNode1 = listNode1 == null ? pHead1 : listNode1.next;
			listNode2 = listNode2 == null ? pHead2 : listNode2.next;
		}

		return listNode1;
	}

}

//其他方法
/*
 * 找出2个链表的长度,然后让长的先走两个链表的长度差,然后再一起走
(因为2个链表用公共的尾部)
public class Solution {
    public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
     if(pHead1==null||pHead2==null){
            return null;
        }
        if(pHead1==pHead2){
            return pHead1;
        }
        int len1=0;
        int len2=0;
        ListNode curr1=pHead1;
        ListNode curr2=pHead2;
        while(curr1!=null){
            len1++;
            curr1=curr1.next;
        }
        while(curr2!=null){
            len2++;
            curr2=curr2.next;
        }
        curr1=pHead1;
        curr2=pHead2;
        if(len1>len2){
            int moreLen=len1-len2;
            while(moreLen!=0){
                curr1=curr1.next;
                moreLen--;
            }
        }
        else{
            int moreLen=len2-len1;
            while(moreLen!=0){
                curr2=curr2.next;
                moreLen--;
            }
        }
 
        while(curr1!=curr2&&curr1!=null){
            curr1=curr1.next;
            curr2=curr2.next;
        }
         
        return curr1;
    }
} 

*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

花月诗人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值