找到链表中第一个公告子节点问题

问题描述:image.png

题目:输入两个链表找到第一个公共子节点

两个链表的头节点已知,相交之后成为一个单链表,相交的位置和相交前的节点个数未知,要求设计一个算法完成。

思考方式:

相信很多人跟我一样拿到题目之后有一点点思路但是又实现不了,这个时候我们考虑将常用的数据结构和算法思想都过一遍,再想想哪些能解决这个问题。

常用的数据结构:数组,链表,队列,栈,Hash表,集合,树,堆等等

常用的算法:各种排序,双指针,递归等等

代码块

public static ListNode findFirstCommonNodeByMap(ListNode pHead1, ListNode pHead2) {
    //1.两个链表有一个为空的情况 则两只链表只能返回空
    if (pHead1 == null || pHead2 == null) {
        return null;
    }
    ListNode current1 = pHead1;
    ListNode current2 = pHead2;

    HashMap<ListNode, Integer> hashMap = new HashMap<ListNode, Integer>();//创建HashMap
    while (current1 != null) {//开始遍历
        hashMap.put(current1, null);
        current1 = current1.next;
    }

    while (current2 != null) {
        if (hashMap.containsKey(current2))
            return current2;
        current2 = current2.next;
    }

    return null;
}
    public static ListNode findFirstCommonNodeByStack(ListNode headA, ListNode headB) {
        Stack<ListNode> stackA = new Stack();
        Stack<ListNode> stackB = new Stack();
        while (headA != null) {
            stackA.push(headA);
            headA = headA.next;
        }
        while (headB != null) {
            stackB.push(headB);
            headB = headB.next;
        }

        ListNode preNode = null;
        while (stackB.size() > 0 && stackA.size() > 0) {
            if (stackA.peek() == stackB.peek()) {
                preNode = stackA.pop();
                stackB.pop();
            } else {
                break;
            }
        }
        return preNode;
    }
public static ListNode findFirstCommonNodeByCombine(ListNode pHead1, ListNode pHead2) {
    if (pHead1 == null || pHead2 == null) {
        return null;
    }
    ListNode p1 = pHead1;
    ListNode p2 = pHead2;
    while (p1 != p2) {
        p1 = p1.next;
        p2 = p2.next;
        if (p1 != p2) {
            if (p1 == null) {
                p1 = pHead2;
            }
            if (p2 == null) {
                p2 = pHead1;
            }
        }
    }
    return p1;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值