160相交链表-hot100

说明:问题描述来源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应该是最优解了。

<think>好的,用户想查找LeetCode热门100题的列表。首先,我需要确认用户的需求,他们可能是在准备面试或者想系统刷题,需要最常被问到的题目列表。LeetCode官网上确实有Hot 100的分类,但用户可能希望得到更直接的信息,比如题目名称、链接或者分类。 接下来,我要考虑如何组织信息。用户可能希望看到题目按难度或类型分类,比如数组、链表、动态规划等。同时,需要提供每个题目的链接,方便用户直接访问。另外,用户提供的引用中有提到一些具体题目,比如146. LRU缓存、169.多数元素、438.找到字符串中所有字母异位词,这些都属于Hot 100,可以作为例子列出。 然后,我需要验证信息的准确性。LeetCodeHot 100可能会随时间变化,但核心题目通常比较稳定。需要确保列出的题目确实属于当前或常见的Hot 100列表。同时,引用中的参考链接和代码示例可能来自LeetCode的题解,需要正确引用。 还要考虑用户可能需要的学习资源,比如推荐的刷题策略或资料。引用中提到的“leetcode hot100与经典150刷题笔记”可以作为推荐资源之一,帮助用户系统学习。 最后,生成相关问题时要覆盖用户可能进一步的需求,比如如何高效刷题、题目分类、具体题目的解题思路等。确保问题能引导用户深入探索,解决他们的潜在疑问。</think>### LeetCode Hot 100题列表 LeetCode Hot 100是平台根据题目被访问和讨论的热度筛选出的高频题目,常用于技术面试准备。以下是部分典型题目分类及示例(完整列表可参考LeetCode官网或第三方整理资源): #### 1. **数组与字符串** - 1. 两数之和(Two Sum) - 15. 三数之和(3Sum) - 42. 接雨水(Trapping Rain Water) - 438. 找到字符串中所有字母异位词(Find All Anagrams in a String)[^2] #### 2. **链表** - 141. 环形链表(Linked List Cycle) - 142. 环形链表 II(Linked List Cycle II)[^1] - 160. 相交链表(Intersection of Two Linked Lists) #### 3. **动态规划** - 70. 爬楼梯(Climbing Stairs) - 121. 买卖股票的最佳时机(Best Time to Buy and Sell Stock) - 139. 单词拆分(Word Break) #### 4. **哈希与集合** - 169. 多数元素(Majority Element)[^3] - 128. 最长连续序列(Longest Consecutive Sequence)[^2] #### 5. **树与图** - 94. 二叉树的中序遍历(Binary Tree Inorder Traversal) - 104. 二叉树的最大深度(Maximum Depth of Binary Tree) - 207. 课程表(Course Schedule) #### 6. **其他高频题** - 146. LRU缓存(LRU Cache)[^1] - 200. 岛屿数量(Number of Islands) - 283. 移动零(Move Zeroes) --- ### 学习资源推荐 1. **官方分类**:访问LeetCode官网,直接查看[Hot 100列表](https://leetcode.cn/problemset/all/?listId=2cktkvj&page=1)。 2. **刷题笔记**:参考整理好的《LeetCode Hot 100与经典150题解析》[^3],包含代码实现和思路解析。 3. **专题训练**:按算法类型(如双指针、滑动窗口)分类刷题,例如环形链表问题使用快慢指针[^1],最长连续序列利用哈希集合优化。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值