两个链表的第一个公共结点

本文介绍了一种高效算法来找到两个链表的第一个公共节点。首先计算两个链表的长度并找出长度差,接着让较长的链表先移动长度差步数,之后两个链表同步移动直至找到第一个相同的节点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

思路:获取两个链表的长度,求的长度差n,让长链表先遍历n次,然后长链表和短链表同时遍历,当长链表和短链表相等时,即为第一公共结点。(O(m1+m2))

public class FindFirstComNode {

    public static ListNote findFirstComNode(ListNote root1,ListNote root2){
        if(root1==null||root2==null){
            return null;
        }
        int len1=getNodeLength(root1);
        int len2=getNodeLength(root2);
        int count=(len1-len2)>0?(len1-len2):(len2-len1);
        ListNote longListNote=(len1-len2)>0?root1:root2;
        ListNote shortListNote=(len1-len2)>0?root2:root1;
        int i=0;
        while((longListNote!=null)&&(shortListNote!=null)&&longListNote!=shortListNote){
            i++;
            longListNote=longListNote.getNext();
            if(i>count){
                shortListNote=shortListNote.getNext();
            }
        }
        ListNote firstComNode=longListNote;
        System.out.println(firstComNode.getValue());
        return firstComNode;
    }
    public static int getNodeLength(ListNote root){
        if(root==null){
            return 0;
        }
        int len=0;
        while(root!=null){
            len++;
            root=root.getNext();            
        }
        return len;
    }
}
### 找到两个链表第一个公共节点 #### 方法一:哈希集合 (基于引用[^1]) 通过使用 `Map` 或者 `Set` 数据结构来存储其中一个链表的所有节点。随后遍历另一个链表,检查当前节点是否已经存在于集合中。如果存在,则该节点即为两链表第一个公共节点。 这种方法的时间复杂度为 \(O(m+n)\),其中 \(m\) 和 \(n\) 是两条链表的长度;空间复杂度为 \(O(m)\) 或 \(O(n)\),取决于哪个链表被存入集合中。 ```python def getIntersectionNode(headA, headB): nodes_in_B = set() current_node = headB while current_node is not None: nodes_in_B.add(current_node) current_node = current_node.next current_node = headA while current_node is not None: if current_node in nodes_in_B: return current_node current_node = current_node.next return None ``` --- #### 方法二:双指针法 (基于引用[^2]) 定义两个指针分别指向两个链表头结点。每次移动一步,当到达链表末端时,跳转至另一条链表头部继续前进。这样可以消除两者之间的长度差,在第二次相遇处即是第一个公共节点。 此方法时间复杂度同样为 \(O(m+n)\),而空间复杂度降为了 \(O(1)\)。 ```python def getIntersectionNode(headA, headB): pointerA, pointerB = headA, headB while pointerA != pointerB: pointerA = headB if pointerA is None else pointerA.next pointerB = headA if pointerB is None else pointerB.next return pointerA # 返回值可能是公共节点或者None ``` --- #### 方法三:计算长度差异并调整起始位置 先遍历两条链表得到它们各自的长度,并求出差值。让较长的那个链表先行走这个差距步数后再同步逐一遍历比较各对应节点直至发现相等为止。 这种方式也实现了线性时间和常量级额外内存消耗的目标。 ```python def length(node): count = 0 while node: count += 1 node = node.next return count def getIntersectionNode(headA, headB): lenA, lenB = length(headA), length(headB) shorter = headA if lenA < lenB else headB longer = headB if lenA < lenB else headA diff = abs(lenA - lenB) for _ in range(diff): longer = longer.next while shorter and longer: if shorter == longer: return shorter shorter = shorter.next longer = longer.next return None ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值