【剑指Offer】面试题52——两个链表的第一个公共节点

1.题目

在这里插入图片描述

2.解法

  1. 暴力解法

    解题思路:类似于两个数组的嵌套循环,找到公共的节点。

    代码实现

    public class Solution {
        public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
            ListNode curA = headA, curB = headB;
            //两层循环
            while(curA != null){
                while(curB != null){
                    //找到相同节点,并返回该节点
                    if(curA == curB){
                        return curB;
                    }
                    curB = curB.next;
                }
                curA = curA.next;
                //注意这里需要将curB复位到headB
                curB = headB;
            }
            return null;
        }
    }
    

    执行结果在这里插入图片描述

    时间复杂度O(n * m)

    空间复杂度O(1)


  2. 哈希表

    解题思路:创建哈希表,将headA放入哈希表中,在哈希表中查找与headB公共的headA节点。

    代码实现

    public class Solution {
        public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
            //创建HashMap,key为ListNode类,value为Boolean型
            HashMap<ListNode, Boolean> map = new HashMap<ListNode, Boolean>();
            ListNode curA = headA;
            ListNode curB = headB;
            //遍历链表A并存储
            while(curA != null){
                map.put(curA, true);
                curA = curA.next;
            }
            //在哈希表中查找公共节点
            while(curB != null){
                if(map.containsKey(curB)){
                    return curB;
                }
                curB = curB.next;
            }
            return null;
        }
    }
    

    执行结果在这里插入图片描述
    时间复杂度O(n)

    空间复杂度O(n)


  3. 快慢指针

    解题思路:计算两链表节点个数之差n,将长链表指针右移n步,然后两链表同时向后查找公共节点。

    代码实现

    public class Solution {
        public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
            if(headA == null || headB == null) return null;
            int n = 0;
            ListNode curA = headA, curB = headB;
            //计数器计量链表A长度
            while(curA.next != null){
                curA = curA.next;
                n++;
            }
            //计数器累减计算A、B两表长度差
            while(curB.next != null){
                curB = curB.next;
                n--;
            }
            //如果A、B链表尾结点不相等,说明不存在公共节点
            if(curA != curB) return null;
            curA = n > 0 ? headA : headB;
            curB = curA == headA ? headB : headA;
            //计数器可能为负值,需要取绝对值!
            n = Math.abs(n);
            //长链表指针右移n步
            while(n > 0){
                curA = curA.next;
                n--;
            }
            //两表同时遍历
            while(curA != curB){
                curA = curA.next;
                curB = curB.next;
            }
            return curA;
        }
    }
    

    执行结果在这里插入图片描述

    时间复杂度O(n+m)

    空间复杂度O(1)


  4. 双指针(最浪漫的相遇)

    解题思路:链表A的指针遍历完链表A后,接着遍历链表B;链表B的指针遍历完链表B后,接着遍历链表A;若存在公共节点,则两个指针相遇的时候,即为公共节点所在。

    代码实现

    public class Solution {
        public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
            ListNode h1 = headA, h2 = headB;
            while(h1 != h2){
                h1 = h1 == null ? headB : h1.next;
                h2 = h2 == null ? headA : h2.next;
            }
            return h1;
        }
    }
    

    执行结果在这里插入图片描述

    时间复杂度O(n+m)

    空间复杂度O(1)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值