两个单链表相交一系列问题

【题目】
在本题中,单链表可能有环,也可能无环。给定两个单链表的头节点head1和head2,这两个链表可能相交,也可能不相交。请实现一个函数, 如果两个链表相交,请返回相交的第一个节点;如果不相交,返回null即可。
要求:如果链表1的长度为N,链表2的长度为M,时间复杂度请达到0(N+M),额外空间复杂度请达到0 (1)。

有环的有两种解法

第一种用set或者map

在这里插入图片描述

 public static ListNode EntryNodeOfLoopBySet(ListNode pHead)
    {
        if(pHead==null||pHead.next==null) return null;
        Set<ListNode> set = new HashSet<>();
        while (pHead!=null){
            if (set.contains(pHead)) return pHead;
            set.add(pHead);
            pHead=pHead.next;
        }
        return null;
    }

不用set的方法

在这里插入图片描述

public static Node getLoopNode(Node head) {
		if (head == null || head.next == null || head.next.next == null) {
			return null;
		}
		Node n1 = head.next; // n1 -> slow
		Node n2 = head.next.next; // n2 -> fast
		while (n1 != n2) {
			if (n2.next == null || n2.next.next == null) {
				return null;
			}
			n2 = n2.next.next;
			n1 = n1.next;
		}
		n2 = head; // n2 -> walk again from head
		while (n1 != n2) {
			n1 = n1.next;
			n2 = n2.next;
		}
		return n1;
	}

无环的两种解法

map方法:遍历链表1存入节点到map,在遍历链表2,如果map有该值那就说交点,很简单不用多说

非map:两个链表分别求出长度len1,len2,然后找到最后两个结束时的节点,last1和last2.如果两者相等一定相交在这里插入图片描述

public static Node noLoop(Node head1, Node head2) {
		if (head1 == null || head2 == null) {
			return null;
		}
		if (loop1 == loop2) { 
			cur1 = head1;
			cur2 = head2;
			int n = 0;
			while (cur1 != loop1) {
				n++;
				cur1 = cur1.next; //cur1到了最后
			}
			while (cur2 != loop2) {
				n--;
				cur2 = cur2.next;//cur2到了最后
			}
			if (cur1 != cur2) {  //如果两者的最后一个节点都不相等,返回null
			return null;
			}
			cur1 = n > 0 ? head1 : head2;   //通过n这个差值来定位长的链表是谁
			cur2 = cur1 == head1 ? head2 : head1; //通过cur1长的旋转来判断cur2短的选择
			n = Math.abs(n);
			while (n != 0) {  //让长的先走n步
				n--;
				cur1 = cur1.next;
			}
			while (cur1 != cur2) { //两个一起走
				cur1 = cur1.next;
				cur2 = cur2.next;
			}
			return cur1;//相遇的时候返回变量
		}
	

两个都有环的解法

有三种情况:第一个头:head1,第一个环loop1.第二格一样
第一种:66型,不想交,loop1 != loop2
第二章:就是无环链表的尾巴多个环,按照无环链表判断即刻
第三种:两根鞭子型,两个链表交于一个圆loop1 == loop2

public static Node bothLoop(Node head1, Node loop1, Node head2, Node loop2) {
		Node cur1 = null;
		Node cur2 = null;
		if (loop1 == loop2) { //如果环相等对应图2  , 然后和上面无环一样
			cur1 = head1;
			cur2 = head2;
			int n = 0;
			while (cur1 != loop1) {
				n++;
				cur1 = cur1.next; //cur1到了最后
			}
			while (cur2 != loop2) {
				n--;
				cur2 = cur2.next;//cur2到了最后
			}
			cur1 = n > 0 ? head1 : head2;   //通过n这个差值来定位长的链表是谁
			cur2 = cur1 == head1 ? head2 : head1; //通过cur1长的旋转来判断cur2短的选择
			n = Math.abs(n);
			while (n != 0) {  //让长的先走n步
				n--;
				cur1 = cur1.next;
			}
			while (cur1 != cur2) { //两个一起走
				cur1 = cur1.next;
				cur2 = cur2.next;
			}
			return cur1;//相遇的时候返回变量
		} else {  //否则判断是图1还是图3
			cur1 = loop1.next;
			while (cur1 != loop1) {//loop1跑
				if (cur1 == loop2) { //如果跑的过程遇见loop2
					return loop1; //那就返回
				}
				cur1 = cur1.next;
			}
			return null; //loop1跑完了还没找到,那就返回null
		}
	}

在这里插入图片描述
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值