[LeetCode]求两个链表的焦点--Intersection of Two Linked Lists

标题题目地址

1.解题意

求解两个链表的焦点,这个交点并不是焦点的值相等,而是需要交点之后的数据是完全相等的。
落实到java层面,就是交点处的对象是同一个对象即可。

ps:我最开始没有读懂题目,然后就开始比较节点的值是否相等,直到示例跑失败了才知道原因。

2.通用解法

    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if(headA!=null && headB!=null){
            ListNode tempA=headA;
            ListNode tempB=headB;
            while(tempA!=null){
                while(tempB!=null){
                    if(tempA==tempB){
                        return tempA;
                    }
                    tempB=tempB.next;
                }
                tempB=headB;
                tempA=tempA.next;
            }
        }
        return null;
    }

这里通过双循环的方式来遍历每一个节点,如果节点是同一个对象即可返回,这是我们正常的思维。
接下来的结果就打脸了:时间复杂度|空间复杂度 超过5%的解法。
哎,还是读题的问题,题目要求了时间复杂度是 O(n),空间复杂度是O(1);那么应该是有更优解的。

3.更优解

首先,假定存在交点,交点之后的链表长度是 c,链表A在交点之前的长度是a,链表B在交点之前的长度是b;
A的长度是 a+c;B的长度是 b+c

如果我们让两边相等,即把长度变成是 a+b+c,那么链表A的最后一个节点是链表B交点之前的节点,即下一个节点就是交点;同理链表B的最后一个节点的下一个节点就是交点;

这样,我们可以只遍历一次,就可以找到交点,最大的长度是 a+b+c+1,即可找到交点;

代码如下:

public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
    ListNode l1 = headA, l2 = headB;
    while (l1 != l2) {
        l1 = (l1 == null) ? headB : l1.next;
        l2 = (l2 == null) ? headA : l2.next;
    }
    return l1;
}

参考:https://cyc2018.github.io/CS-Notes/#/notes/Leetcode%20%E9%A2%98%E8%A7%A3%20-%20%E9%93%BE%E8%A1%A8

原文地址:https://www.cnblogs.com/paxing/p/11260338.html

时间: 2024-11-09 02:17:46

[LeetCode]求两个链表的焦点--Intersection of Two Linked Lists的相关文章

哈希(4) - 求两个链表的交集(intersection)以及并集(union)

给定两个链表,求它们的交集以及并集.用于输出的list中的元素顺序可不予考虑. 例子: 输入下面两个链表: list1: 10->15->4->20 list2: 8->4->2->10 输出链表: 交集list: 4->10 并集list: 2->8->20->4->15->10 方法1 (简单方法) 可以参考链表系列中的"链表操作 - 求两个链表的交集(intersection)以及并集(union)" 方法2

【LeetCode从零单排】No.160 Intersection of Two Linked Lists

题目 Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 begin to intersect at node c1. Notes: If the two linked lists have n

[LeetCode] Intersection of Two Linked Lists 求两个链表的交点

Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 begin to intersect at node c1. Notes: If the two linked lists have no i

【LeetCode每天一题】 Intersection of Two Linked Lists(两个链表的入口节点)

Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: begin to intersect at node c1. Example 1: Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5],

[LintCode] Intersection of Two Linked Lists 求两个链表的交点

Write a program to find the node at which the intersection of two singly linked lists begins. Notice If the two linked lists have no intersection at all, return null. The linked lists must retain their original structure after the function returns. Y

leetCode 160. Intersection of Two Linked Lists 链表

160. Intersection of Two Linked Lists Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: A:          a1 → a2                                          c1 → c2 → c3

lintcode 中等题:Intersection of Two Linked Lists 两个链表的交叉

题目 两个链表的交叉 请写一个程序,找到两个单链表最开始的交叉节点. 样例 下列两个链表: A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 在节点 c1 开始交叉. 注意 如果两个链表没有交叉,返回null. 在返回结果后,两个链表仍须保持原有的结构. 可假定整个链表结构中没有循环. 挑战 需满足 O(n) 时间复杂度,且仅用 O(1) 内存. 解题 尝试用时间复杂度是O(NM),却没有解决,在这个博客看到根据两个链表的特性进行解决. 就如同上图,两个链表相交的部

[LeetCode] 160. Intersection of Two Linked Lists 解题思路

Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 begin to intersect at node c1. Notes: If the two linked lists have no i

LeetCode——Intersection of Two Linked Lists

Description: Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 begin to intersect at node c1. 求两个链表的相交的部分 /** * Definitio