160相交链表-hot100

48 篇文章 0 订阅
2 篇文章 0 订阅

说明:问题描述来源leetcode

一、问题描述:

给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表相交的起始节点。如果两个链表不存在相交节点,返回 null 。
图示两个链表在节点 c1 开始相交:  题目数据 保证 整个链式结构中不存在环。    注意,函数返回结果后,链表必须 保持其原始结构 。
自定义评测:
评测系统 的输入如下(你设计的程序 不适用 此输入):
intersectVal - 相交的起始节点的值。如果不存在相交节点,这一值为 0
listA - 第一个链表
listB - 第二个链表
skipA - 在 listA 中(从头节点开始)跳到交叉节点的节点数
skipB - 在 listB 中(从头节点开始)跳到交叉节点的节点数
评测系统将根据这些输入创建链式数据结构,并将两个头节点 headA 和 headB 传递给你的程序。如果程序能够正确返回相交节点,
那么你的解决方案将被 视作正确答案 。

示例 1:
输入:intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3
输出:Intersected at '8'
解释:相交节点的值为 8 (注意,如果两个链表相交则不能为 0)。
从各自的表头开始算起,链表 A 为 [4,1,8,4,5],链表 B 为 [5,6,1,8,4,5]。
在 A 中,相交节点前有 2 个节点;在 B 中,相交节点前有 3 个节点。
— 请注意相交节点的值不为 1,因为在链表 A 和链表 B 之中值为 1 的节点 (A 中第二个节点和 B 中第三个节点) 是不同的节点。
换句话说,它们在内存中指向两个不同的位置,而链表 A 和链表 B 中值为 8 的节点 (A 中第三个节点,B 中第四个节点) 在内存中指向相同的位置。
示例 2:
输入:intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
输出:Intersected at '2'
解释:相交节点的值为 2 (注意,如果两个链表相交则不能为 0)。
从各自的表头开始算起,链表 A 为 [1,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 。
提示:listA 中节点数目为 m;listB 中节点数目为 n;1 <= m, n <= 3 * 104;1 <= Node.val <= 105;0 <= skipA <= m;0 <= skipB <= n
如果 listA 和 listB 没有交点,intersectVal 为 0      如果 listA 和 listB 有交点,intersectVal == listA[skipA] == listB[skipB]

二、题解:

思路1(失败)

题解+测试

public class Solution {
    /**
     * 这个方法应该不行,应该是发生了哈希冲突,真的是卧槽了呀
     * @param headA
     * @param headB
     * @return
     */
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        Set<Integer> set = new HashSet<>();
        ListNode current1 = headA;
        ListNode current2 = headB;

        while (current1 != null && current2 != null) {
            if (current1 != null) {
                if (!set.add(current1.hashCode())) return current1;
                current1 = current1.next;
            }

            if (current2 != null) {
                if (!set.add(current2.hashCode())) return current1;

                current2 = current2.next;
            }
        }

        return null;
    }

    @Test
    public void testSolution() {
        ListNode node1 = new ListNode(8, new ListNode(4, new ListNode(5)));

        ListNode node2 = new ListNode(2, new ListNode(4));

        ListNode[][] listNodes = {
                {
                        new ListNode(4, new ListNode(1, node1)), new ListNode(5, new ListNode(6, new ListNode(1, node1)))
                },
                {
                        new ListNode(1, new ListNode(9, new ListNode(1, node2))), new ListNode(3, node2)
                },
                {
                        new ListNode(2, new ListNode(6, new ListNode(4))), new ListNode(1, new ListNode(5))
                }
        };

        for (int i = 0; i < listNodes.length; i++) {
            ListNode res = getIntersectionNode(listNodes[i][0], listNodes[i][1]);
            System.out.println("第" + (i + 1) + "个的结果是" + (res == null ? null : res.val));
        }
    }
}

反思:

这里的是出现了哈希冲突的问题。

本来的思路是使用哈希表,这里的哈希表就用set集合来添加节点吧。是在在两个链表上同时添加的,也就是一次添加节点到哈希表时,是分别添加一个在链表a的节点和一个在链表b上的节点,当添加时第一次遇到重复的元素,则说明这个元素是交叉节点。

但是出现哈希冲突了,

用hashCode也是一样离谱的:

public class Solution {
    /**
     * 这个方法应该不行,应该是发生了哈希冲突,真的是卧槽了呀
     * @param headA
     * @param headB
     * @return
     */
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        Set<ListNode> set = new HashSet<>();
        ListNode current1 = headA;
        ListNode current2 = headB;

        while (current1 != null && current2 != null) {
            if (current1 != null) {
                if (!set.add(current1)) return current1;
                current1 = current1.next;
            }

            if (current2 != null) {
                if (!set.add(current2)) return current1;

                current2 = current2.next;
            }
        }

        return null;
    }

    @Test
    public void testSolution() {
        ListNode node1 = new ListNode(8, new ListNode(4, new ListNode(5)));

        ListNode node2 = new ListNode(2, new ListNode(4));

        ListNode[][] listNodes = {
                {
                        new ListNode(4, new ListNode(1, node1)), new ListNode(5, new ListNode(6, new ListNode(1, node1)))
                },
                {
                        new ListNode(1, new ListNode(9, new ListNode(1, node2))), new ListNode(3, node2)
                },
                {
                        new ListNode(2, new ListNode(6, new ListNode(4))), new ListNode(1, new ListNode(5))
                }
        };

        for (int i = 0; i < listNodes.length; i++) {
            ListNode res = getIntersectionNode(listNodes[i][0], listNodes[i][1]);
            System.out.println("第" + (i + 1) + "个的结果是" + (res == null ? null : res.val));
        }
    }
}

思路2(失败)

还有什么?还有谁!!!

public class Solution2 {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        List<ListNode> list1 = new LinkedList<>();
        List<ListNode> list2 = new LinkedList<>();
        List<ListNode> tmp;
        ListNode curr1 = headA;
        ListNode curr2 = headA;
        while (curr1 != null) {
            list1.add(curr1);
            curr1 = curr1.next;
        }
        while (curr2 != null) {
            list2.add(curr2);
            curr2 = curr2.next;
        }
        if (list1.size() > list2.size()) {
            tmp = list1;
            list1 = list2;
            list2 = tmp;
        }
        int index = list2.size() - 1;
        for (int i = list1.size() - 1; i >= 0; i--) {
            if (list1.get(i) != list2.get(index)) return list1.get(i + 1);
            index--;
        }
        return null;
    }
}

这个真不知道为什么不行!!!

反思:

可能是可能加入到链表后,对于一些地址不是改变了,而是有些不知道的变化。有些猜想,不知道对不对的。

再来:

思路3

题解3:

public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode current = headA;
        int lenA = 0;
        while (current != null){//先找出链表A的长度吧
            lenA++;
            current = current.next;
        }
        current = headB;
        int lenB = 0;
        while (current != null){//再找出链表B的长度
            lenB++;
            current = current.next;
        }
        //下面肯定是先遍历长的a,怎么确定长的是哪一个呢?
        ListNode listNode1 = headA;
        ListNode listNode2 = headB;
        if (lenA < lenB){
            ListNode tmp = headA;
            listNode1 = headB;
            listNode2 = tmp;
            int tmpInt = lenA;
            lenA = lenB;
            lenB = tmpInt;
        }
        int diff = lenA - lenB;
        while (diff > 0){//当list1遍历到还剩下lenB个节点时就停下
            listNode1 = listNode1.next;
            diff--;
        }
        while (listNode1 != null){//开始对齐比较
            if (listNode1 == listNode2) return listNode1;
            listNode1 = listNode1.next;
            listNode2 = listNode2.next;
        }
        return null;
    }

}

这个思路很明显的了,就是想要找到长度对齐的链表开始比较,直到找到相交的节点。

下面来细说一下:

对齐比较:对于两个链表,如果他们的长度相等那么就可以对齐比较,那么可以任选一个链表作为参照系,直接遍历这个链表和另一个链表,每次要将链表1和链表2指向他们链表的下一个节点。遍历过程要判断当前链表的节点和另一个链表的节点的地址是否相等,如果相等那么就返回该节点。

但我们不一定保证两个链表的长度,那么就要去先分别遍历这两个链表,找出链表的长度。将较长的链表设置为list1,长度为lenA,短的链表设置为list2,长度为lenB;然后因为链表list1多list2长(lenA - lenB)个节点,于是将先遍历list1,使得list1遍历到还剩下lenB个节点时再开始进行两个链表的对齐比较

三、反思与总结

对于这个题,不知道之前有没有遇到过,思路1和思路2不知道为什么错了,现在没啥时间所以后面有时间再来完善和揪出为什么思路1和思路2不行的原因吧。另外思路1和思路2的空间复杂度也是挺大的,对于使用到的数据结构也是相当有重量级一些的,于是在时间复杂度上应该也不太行。目前就思路3是搞定了,思路3应该是最优解了。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值