Leetcode:面试题 02.07. 链表相交

给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表相交的起始节点。如果两个链表没有交点,返回 null 。

图示两个链表在节点 c1 开始相交

题目数据 保证 整个链式结构中不存在环。

注意,函数返回结果后,链表必须 保持其原始结构 。

示例 1:

输入:intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
输出:Intersected at '8'
解释:相交节点的值为 8 (注意,如果两个链表相交则不能为 0)。
从各自的表头开始算起,链表 A 为 [4,1,8,4,5],链表 B 为 [5,0,1,8,4,5]。
在 A 中,相交节点前有 2 个节点;在 B 中,相交节点前有 3 个节点。

示例 2:

输入:intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
输出:Intersected at '2'
解释:相交节点的值为 2 (注意,如果两个链表相交则不能为 0)。
从各自的表头开始算起,链表 A 为 [0,9,1,2,4],链表 B 为 [3,2,4]。
在 A 中,相交节点前有 3 个节点;在 B 中,相交节点前有 1 个节点。

示例 3:

输入:intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
输出:null
解释:从各自的表头开始算起,链表 A 为 [2,6,4],链表 B 为 [1,5]。
由于这两个链表不相交,所以 intersectVal 必须为 0,而 skipA 和 skipB 可以是任意值。
这两个链表不相交,因此返回 null 。

解法1:计算相交后的长度

public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode curA=headA;
        ListNode curB=headB;
        
        //如果有公共的解的话 c是公共的长度: A =a+(b-c) ,B =b+ (a-c)
        //如果A遍历完,就开始遍历B的头,B先遍历完,也是要接着遍历A的头在,直到找到相交的点
        while(curA !=curB){
            curA = curA ==null ? headB : curA.next;
            curB = curB == null ? headA : curB.next;
        }
        return curA;
    }
}

解法2:直接模拟;

我们求出两个链表的长度,并求出两个链表长度的差值,然后让curA移动到,和curB 末尾对齐的位置

public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        int A_len=0;
        int B_len=0;
        ListNode curA = new ListNode();
        ListNode curB=new ListNode();
        curA=headA;
        curB=headB;
        while (curA!=null){ 
            curA=curA.next;
            A_len++;
        }

        while (curB !=null){
            curB=curB.next;
            B_len++;
        }
        //重新回到各自的头节点
        curA=headA;
        curB=headB;
        if (A_len==B_len) {   
           for (int i=0;i<A_len;i++){
               if (curA ==curB){
                  return curA;
               }
               curA=curA.next;
               curB=curB.next;
           }
        }else if(A_len >B_len){

            //如果A 比B要长,先移动A到与B末尾相对齐的节点;
            int dislength=A_len-B_len;
            for (int i=0;i<dislength;i++){
                curA=curA.next;
            }
            for (int j=0;j<=B_len;j++){
                if (curA==curB) {
                    return curA;
                }
                curA=curA.next;
                curB=curB.next;
            }
        }else{     
            int dislength=B_len-A_len;
            for (int i=0;i<dislength;i++){
                curB=curB.next;
            }
            
            for(int j=0;j<A_len;j++){
                if (curA==curB)
                    return curA;
                curA=curA.next;
                curB=curB.next;
            }
        }

        return null;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值