160. 相交链表 - [简单]

力扣icon-default.png?t=M276https://leetcode-cn.com/problems/intersection-of-two-linked-lists/solution/xiang-jiao-lian-biao-by-leetcode-solutio-a8jn/

方法一:哈希 

package com.company.linked;

import java.util.HashSet;
import java.util.Set;

public class Solution3 {

    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {

        Set<ListNode> noRepeatSet = new HashSet<>();

        while (headA != null || headB != null) {
            if (headA != null) {
                if (noRepeatSet.contains(headA)) {
                    return headA;
                }
                noRepeatSet.add(headA);
                headA = headA.next;
            }


            if (headB != null) {
                if (noRepeatSet.contains(headB)) {
                    return headB;
                }
                noRepeatSet.add(headB);
                headB = headB.next;
            }
        }

        return null;

    }
}

方法二:双指针

  1. 求出两个链表的长度
  2. 将更长的链表长的几个节点忽略,然后两个链表逐步遍历,遇到相等节点则表示相交
package com.company.linked;

public class Solution3_1 {

    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode pA = headA;
        ListNode pB = headB;

        int aCount = 0, bCount = 0;

        while (pA != null) {
            aCount++;
            pA = pA.next;
        }
        pA = headA;


        while (pB != null) {
            bCount++;
            pB = pB.next;
        }
        pB = headB;

        if (aCount > bCount) {
            for (int i = 0; i < aCount - bCount; i++) {
                pA = pA.next;
            }

            while (pA != null && pB != null) {
                if (pA == pB) {
                    return pA;
                }

                pA = pA.next;
                pB = pB.next;
            }

        } else if (aCount < bCount) {
            for (int i = 0; i < bCount - aCount; i++) {
                pB = pB.next;
            }

            while (pA != null && pB != null) {
                if (pA == pB) {
                    return pA;
                }

                pA = pA.next;
                pB = pB.next;
            }
        } else {
            while (pA != null && pB != null) {
                if (pA == pB) {
                    return pA;
                }

                pA = pA.next;
                pB = pB.next;
            }

        }

        return null;
    }
}

  

 方法三:双指针

  • 两个链表逐个遍历比较
  • 只要有一个节点先等于null,即先走到尾部,则将当前遍历指针替换成另外一个链表的头;
  • 这样循环往复两个指针一定会相遇,因为则将当前遍历指针替换成另外一个链表的头节点可以类似于人工加了一个环,然后两个一定会相遇
package com.company.linked;

public class Solution3_2 {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if (headA == null || headB == null) {
            return null;
        }

        ListNode pA = headA, pB = headB;

        while (pA != pB) {
            if (pA == null) {
                pA = headB;
            } else {
                pA = pA.next;
            }


            if (pB == null) {
                pB = headA;
            } else {
                pB = pB.next;
            }
        }
        return pA;
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值