面试题 02.07. 链表相交

面试题 02.07. 链表相交

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

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

img

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

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

示例 1:

img

输入: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:

img

输入: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:

img

输入: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 。

思考

解题思路:

设「第一个公共节点」为 node ,「链表 headA」的节点数量为a ,「链表 headB」的节点数量为 b ,「两链表的公共尾部」的节点数量为 c ,则有:

  • 头节点 headAnode 前,共有 a - c 个节点;
  • 头节点 headBnode 前,共有 b - c个节点;

Picture1.png

考虑构建两个节点指针 A ,B 分别指向两链表头节点 headA , headB ,做如下操作:

指针 A先遍历完链表headA ,再开始遍历链表headB ,当走到 node 时,共走步数为:
a + ( b − c ) a + (b - c) a+(bc)

指针 B 先遍历完链表 headB ,再开始遍历链表 headA ,当走到 node 时,共走步数为:
b + ( a − c ) b + (a - c) b+(ac)

如下式所示,此时指针 A , B 重合,并有两种情况:

  • 若两链表 有 公共尾部 (即 c > 0 ) :指针 A , B 同时指向「第一个公共节点」node
  • 若两链表 无 公共尾部 (即 c = 0 ) :指针A , B 同时指向 null
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if(headA==null||headB==null) return null;
        ListNode tempA=headA;
        ListNode tempB=headB;
        while(tempA!=tempB){
            // 如果两个指针的下一个同时为空,证明都完成了a+b遍历
            if(tempA.next==null&&tempB.next==null) return null; 
            if(tempA.next!=null) tempA=tempA.next;
            else tempA=headB;
            if(tempB.next!=null) tempB=tempB.next;
            else tempB=headA;
        }
        return tempA;
    }
  • 遍历两个链表,构建反向节点链表。

    • 反向节点链表保存链表地址
  • 遍历反向链表,直到节点不相等,这里节点对比的是地址。

不过这个方法效率较低。

package 力扣;

import javax.sound.midi.Soundbank;

/**
 * @author yyq
 * @create 2022-03-10 9:56
 */
public class mst0207 {
    public static void main(String[] args) {
        ListNode headA=new ListNode(1);
        headA.next= new ListNode(2);
        ListNode headB=new ListNode(3);
        headB.next=headA.next;
        getIntersectionNode(headA,headB);
    }
    public static ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if(headA==null||headB==null) return null;
        if(headA==headB) return headA;
        ListNodeMST Ahead=reverseList(headA);
        ListNodeMST Bhead=reverseList(headB);
        if(Ahead.val!=Bhead.val) return null;
        while (Ahead.val==Bhead.val){
            if(Ahead.next!=null&&Bhead.next!=null&&Ahead.next.val==Bhead.next.val){
                Ahead=Ahead.next;
                Bhead=Bhead.next;
            }
            else {
                System.out.println("Intersected at '"+Ahead.val.val+"'");
                return Ahead.val;
            }
        }
        return null;
    }
    public static ListNodeMST reverseList(ListNode head){
        ListNodeMST Ahead=null;
        ListNode temp=head;
        while(temp!=null){
            Ahead=new ListNodeMST(temp,Ahead);
            temp=temp.next;
        }
        return Ahead;
    }

}
class ListNodeMST {
    ListNode val;
    ListNodeMST next;
    ListNodeMST() {}

    public ListNodeMST(ListNode val, ListNodeMST next) {
        this.val = val;
        this.next = next;
    }
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值