Skip to content
本页目录

52-两个链表的第一个公共节点

题目描述

https://leetcode.cn/problems/liang-ge-lian-biao-de-di-yi-ge-gong-gong-jie-dian-lcof 输入两个链表,找出它们的第一个公共节点。

如下面的两个链表:

在节点 c1 开始相交。

示例 1:

img

输入:intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
输出:Reference of the node with value = 8
输入解释:相交节点的值为 8 (注意,如果两个列表相交则不能为 0)。从各自的表头开始算起,链表 A 为 [4,1,8,4,5],链表 B 为 [5,0,1,8,4,5]。在 A 中,相交节点前有 2 个节点;在 B 中,相交节点前有 3 个节点。

示例 2:

输入:intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
输出:Reference of the node with value = 2
输入解释:相交节点的值为 2 (注意,如果两个列表相交则不能为 0)。从各自的表头开始算起,链表 A 为 [0,9,1,2,4],链表 B 为 [3,2,4]。在 A 中,相交节点前有 3 个节点;在 B 中,相交节点前有 1 个节点。

示例 3:

img

输入: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。

注意:

  • 如果两个链表没有交点,返回 null.
  • 在返回结果后,两个链表仍须保持原有的结构。
  • 可假定整个链表结构中没有循环。
  • 程序尽量满足 O(n) 时间复杂度,且仅用 O(1) 内存。
  • 本题与主站 160 题相同:https://leetcode.cn/problems/intersection-of-two-linked-lists/

思路1 先求出最短长度

基本思路: 先求出两个链表的最短长度n,那么他们最长就可以倒数n的地方相同 如果不相同则 n+1 直到相同位置,如果不相同,则没有相同节点

实现代码

csharp
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public int val;
 *     public ListNode next;
 *     public ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode GetIntersectionNode(ListNode headA, ListNode headB) {
        // 判断边界
        if(headA == null || headB == null){
            return null;
        }
        // 获取 A 和 B 的长度
        int countA = 0;
        int countB = 0;
        ListNode cur = headA;
        while(cur != null){
            countA++;
            cur = cur.next;
        }
        cur = headB ;
        while(cur != null){
            countB++;
            cur = cur.next;
        }
        int count;
        
        ListNode shortHead, longHead;
        if(countA > countB){
            count = countB;
            shortHead = headB;
            longHead = headA;
        }
        else{
            count = countA;
            shortHead = headA;
            longHead = headB;
        }
        
        //顺延判断
        int preCount = Math.Abs(countA - countB);
        Console.WriteLine("count:{0},preCount:{1}",count,preCount);
        for(int i=0; i<preCount; i++){
            longHead = longHead.next;
        }
        for(int i=0; i<count; i++){
            //此处应该判断引用
            if(shortHead == longHead){
                return shortHead;
            }
            else{
                shortHead = shortHead.next;
                longHead = longHead.next;
            }
        }
        return null;
    }
}

思路2 双链表拼接:官方解法

思路2 :官方解法,双链表拼接 思路非常巧妙, 将两个链表拼接,则长度一样,如果有相交节点,一定会在某处相遇 指针相等,若没有 指针会同时变成null 设交集链表长c,链表1除交集的长度为a,链表2除交集的长度为b,有 若无交集,则a + b = b + a

注意1 curA 到末尾的时候,要重新指向 headB,不是 curB 注意2 while 循环判断的是 curA != curB 而不是他们是否为 null 注意3 curA 复制的时候,要先只想 next 节点,然后下次判断自身是否为null,不能直接判断next是null就指向 headB 这样会死循环

csharp
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public int val;
 *     public ListNode next;
 *     public ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode GetIntersectionNode(ListNode headA, ListNode headB) {
        if(headA == null || headB == null){
            return null;
        }
        ListNode curA = headA;
        ListNode curB = headB;
        while(curA != curB){
            curA = curA != null ? curA.next : headB;
            curB = curB != null ? curB.next : headA;
        }
        return curA;
    }
}

Released under the MIT License.