leetcode 160 相交链表

给你两个单链表的头节点 headAheadB ,请你找出并返回两个单链表相交的起始节点。如果两个链表没有交点,返回 null

图示两个链表在节点 c1 开始相交

题目数据 保证 整个链式结构中不存在环。

注意,函数返回结果后,链表必须 保持其原始结构

示例 1:

输入:intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
输出:Intersected at '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
输出:Intersected at '2'
解释:相交节点的值为 2 (注意,如果两个链表相交则不能为 0)。
从各自的表头开始算起,链表 A 为 [0,9,1,2,4],链表 B 为 [3,2,4]。
在 A 中,相交节点前有 3 个节点;在 B 中,相交节点前有 1 个节点。

示例 3:

输入: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 。
package com.example.lettcode.dailyexercises;

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

/**
 * @Class GetIntersectionNode
 * @Description 160 相交链表
 * @Author
 * @Date 2021/6/4
 **/
public class GetIntersectionNode {
    static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
            next = null;
        }
    }

    /**
     * 方法1:两个指针将两个链表遍历一遍,如果中途相遇,说明相遇节点就是相交节点
     *
     * @param headA
     * @param headB
     * @return
     */
    public static ListNode getIntersectionNode01(ListNode headA, ListNode headB) {
        if (headA == null || headB == null) return null;
        ListNode pA = headA, pB = headB;

        while (pA != pB) {
            pA = pA == null ? headB : pA.next;
            pB = pB == null ? headA : pB.next;
        }
        return pA;
    }

    /**
     * 方法2:先找出长度较长的链表,然后指向较长链表的指针先走若干步,使得两个链表后续未遍历的长度相等
     * 超时
     *
     * @param headA
     * @param headB
     * @return
     */
    public static ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if (headA == null || headB == null) return null;

        int lenA = 0;
        ListNode listNodeA = headA;
        while (listNodeA != null) {
            listNodeA = listNodeA.next;
            lenA++;
        }
        int lenB = 0;
        ListNode listNodeB = headB;
        while (listNodeB != null) {
            listNodeB = listNodeB.next;
            lenB++;
        }
        ListNode listNodeF = lenA >= lenB ? headA : headB;
        ListNode listNodeS = lenA >= lenB ? headB : headA;
        int len = lenA >= lenB ? lenA - lenB : lenB - lenA;
        while (len != 0 && listNodeF != null) {
            listNodeF = listNodeF.next;
            len--;
        }

        while (listNodeF != null && listNodeS != null) {
            // 应该是判断两个节点是否指向同一位置,而不仅判断两者元素值
            if (listNodeF == listNodeS) return listNodeF;
            listNodeF = listNodeF.next;
            listNodeS = listNodeS.next;
        }
        return null;
    }
}	
// 测试用例
public static void main(String[] args) {
    // intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
	ListNode headA = new ListNode(4);
	ListNode listNodeA1 = new ListNode(1);
	headA.next = listNodeA1;
	ListNode listNodeA2 = new ListNode(8);
	listNodeA1.next = listNodeA2;
	ListNode listNodeA3 = new ListNode(4);
	listNodeA2.next = listNodeA3;
	ListNode listNodeA4 = new ListNode(5);
	listNodeA3.next = listNodeA4;

	ListNode headB = new ListNode(5);
	ListNode listNodeB1 = new ListNode(0);
	headB.next = listNodeB1;
	ListNode listNodeB2 = new ListNode(1);
	listNodeB1.next = listNodeB2;
	ListNode listNodeB3 = new ListNode(8);
	listNodeB2.next = listNodeB3;
	ListNode listNodeB4 = new ListNode(4);
	listNodeB3.next = listNodeB4;
	ListNode listNodeB5 = new ListNode(5);
	listNodeB4.next = listNodeB5;

	ListNode listNode = GetIntersectionNode.getIntersectionNode(headA, headB);
	System.out.print("GetIntersectionNode demo01 result : ");
	if (listNode != null) {
		System.out.println(listNode.val);
	} else {
		System.out.println("NO found intersectionNode!!");
	}
	// 0,9,1,2,4], listB = [3,2,4
	headA = new ListNode(0);
	listNodeA1 = new ListNode(9);
	headA.next = listNodeA1;
	listNodeA2 = new ListNode(1);
	listNodeA1.next = listNodeA2;
	listNodeA3 = new ListNode(2);
	listNodeA2.next = listNodeA3;
	listNodeA4 = new ListNode(4);
	listNodeA3.next = listNodeA4;

	headB = new ListNode(3);
	listNodeB1 = new ListNode(2);
	headB.next = listNodeB1;
	listNodeB2 = new ListNode(4);
	listNodeB1.next = listNodeB2;
	listNode = GetIntersectionNode.getIntersectionNode(headA, headB);
	System.out.print("GetIntersectionNode demo02 result : ");
	if (listNode != null) {
		System.out.println(listNode.val);
	} else {
		System.out.println("NO found intersectionNode!!");
	}

	//  [2,6,4], listB = [1,5]
	headA = new ListNode(2);
	listNodeA1 = new ListNode(6);
	headA.next = listNodeA1;
	listNodeA2 = new ListNode(4);
	listNodeA1.next = listNodeA2;

	headB = new ListNode(1);
	listNodeB1 = new ListNode(5);
	headB.next = listNodeB1;
	listNode = GetIntersectionNode.getIntersectionNode(headA, headB);
	System.out.print("GetIntersectionNode demo03 result : ");
	if (listNode != null) {
		System.out.println(listNode.val);
	} else {
		System.out.println("NO found intersectionNode!!");
	}
}
原文地址:https://www.cnblogs.com/fyusac/p/14849834.html