[LC]141题 Intersection of Two Linked Lists (相交链表)(链表)

①中文题目

编写一个程序,找到两个单链表相交的起始节点。

如下面的两个链表:

在节点 c1 开始相交。

注意:

如果两个链表没有交点,返回 null.
在返回结果后,两个链表仍须保持原有的结构。
可假定整个链表结构中没有循环。
程序尽量满足 O(n) 时间复杂度,且仅用 O(1) 内存。

②思路

遍历,O(M*N)的遍历,直到找出相等的结点(不是val相等)

③我的代码(很慢)

 1 public class Solution {
 2     public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
 3         ListNode temp1=headA;
 4         ListNode temp2=headB;
 5         if(temp1==null||temp2==null)
 6             return null;
 7         while(temp1!=null){
 8             if(temp2==null){   //当temp2遍历到尾巴的时候,
 9                 temp1=temp1.next;    //temp1后移1位
10                 temp2=headB;       //temp2回到自己原本的头部,再来一遍
11             }
12             if(temp1==temp2)
13                 return temp1;
14             temp2=temp2.next;     //这一次比较,没找到相等的,于是temp2后移一步
15         }
16         return null;
17     }
18 }

④运行结果

执行结果:通过 。显示详情

执行用时 :674 ms, 在所有 Java 提交中击败了5.01%的用户

内存消耗 :48.5 MB, 在所有 Java 提交中击败了5.05%的用户

⑤别人的快速代码

 1 public class Solution {
 2     public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
 3      if (headA == null || headB == null) return null;
 4       ListNode pA = headA, pB = headB;
 5       while (pA != pB) {
 6          pA = pA == null ? headB : pA.next;
 7          pB = pB == null ? headA : pB.next;
 8        }
 9       return pA;
10     }
11 }

他的运行结果:

执行结果:

通过

显示详情

执行用时 :1 ms, 在所有 Java 提交中击败了100.00%的用户

内存消耗 :37.5 MB, 在所有 Java 提交中击败了85.92%的用户

他的思路:https://leetcode-cn.com/problems/intersection-of-two-linked-lists/solution/tu-jie-xiang-jiao-lian-biao-by-user7208t/。太厉害了。

⑥学到的知识

1、我自己的代码里,12行,不是val元素相等,而是要判断结点是否相等(结点包括元素和指针)

2、看看别人的代码里的思路,太厉害了。还给出了图解!

原文地址:https://www.cnblogs.com/zf007/p/11609902.html

时间: 2024-07-29 12:37:29

[LC]141题 Intersection of Two Linked Lists (相交链表)(链表)的相关文章

leetcode_160题——Intersection of Two Linked Lists(线性表,哈希表)

Intersection of Two Linked Lists Total Accepted: 28581 Total Submissions: 100989My Submissions Question Solution Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists

【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 两个链表的交叉

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

160 Intersection of Two Linked Lists 相交链表

编写一个程序,找到两个单链表相交的起始节点.例如,下面的两个链表:A:           a1 → a2                                                            c1 → c2 → c3                                        B:  b1 → b2 → b3在节点 c1 开始相交.注意:    如果两个链表没有交点,返回 null.    在返回结果后,两个链表仍须保持原有的结构.    可假

[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

[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

【leetcode】Intersection of Two Linked Lists

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:

leetcode Intersection of Two Linked Lists python

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. python