160. Intersecting Linked List-Java Implementation

Title description

Write a program to find the starting node where two singly linked lists intersect.

Such as the following two linked lists:

Insert picture description here

The intersection starts at node c1.

 

Example 1:

Insert picture description here

Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA= 2, skipB = 3
Output: Reference of the node with Value = 8
Input explanation: The value of the intersection node is 8 (note that if two linked lists intersect, it cannot be 0). Starting from the respective headers, linked list A is [4,1,8,4,5], and linked list B is [5,0,1,8,4,5]. In A, there are 2 nodes before the intersecting node; in B, there are 3 nodes before the intersecting node.

 

Example 2:

Insert picture description here

Input: intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
Output: Reference of the node with value = 2
Input explanation: The value of the intersection node is 2 (note that if two linked lists intersect, it cannot be 0). Starting from the respective headers, linked list A is [0,9,1,2,4], and linked list B is [3,2,4]. In A, there are 3 nodes before the intersecting node; in B, there is 1 node before the intersecting node.

 

Example 3:

Insert picture description here

Input: intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB= 2
Output: null
Input explanation: Counting from the respective header, linked list A is [ 2,6,4], linked list B is [1,5]. Since these two linked lists do not intersect, intersectVal must be 0, and skipA and skipB can be arbitrary values. Explanation: The two linked lists do not intersect, so null is returned.

 

note:

If there is no intersection between the two linked lists, null is returned. After the result is returned, the two linked lists must still maintain the original structure. It can be assumed that there are no cycles in the entire linked list structure. The program tries to satisfy O(n) time complexity, and only uses O(1) memory.

 

Solution 1: Brute force (double loop)

For each element in linked list A, go to linked list B to find whether it exists, and it needs to be executed m*n times

public class Solution {
    
    
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
    
    
        // 如果其中一个为空,返回null
        if (headA == null || headB == null) return;
        
        ListNode A = headA;
        
        while (null != A) {
    
    
            ListNode B = headB;
            while (null != B){
    
    
                // 如果相等则返回交点
                if (B == A){
    
    
                    return A;
                }
                // 不相等则继续循环链表B
                B = B.next;
            }
            // 单次循环结束,A链表前移
            A = A.next;
        }
        return null;
    }
}

Time complexity: O(mn), m is the length of linked list A, n is the length of linked list B.
Space complexity: O(1)

 

Solution 2: Hash table

Traverse the elements in the linked list A, refer to the elements stored in the HashMap and
traverse the linked list B, check whether the element exists in the HashMap in turn, return if it exists, and return null at the end of the traversal

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

        HashMap<ListNode, Integer> map = new HashMap<>();

        // 将链表A中数据存入map中
        while (null != headA){
    
    
            map.put(headA, headA.val);
            headA = headA.next;
        }
        // 遍历链表B,在map中查找是否存在,若存在则返回该结点
        while (null != headB){
    
    
            if (map.containsKey(headB)){
    
    
                return headB;
            }
            headB = headB.next;
        }
        // 链表B遍历结束,返回null
        return null;
    }
}

Time complexity: O(m+n), m is the length of linked list A, n is the length of linked list B.
Space complexity: O(m) or O(n)

 

Solution 3: Double pointer

The idea of ​​this solution is:
A and B two linked lists have different lengths. If the two linked lists have an intersection, the distance traveled when they intersect is the same
, that is, A+B = B+A, so traverse A+B and traverse B+A must Is ending at the same time

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

        ListNode A = headA, B = headB;
        // 当没有相交时循环
        while (A != B) {
    
    
            // 链表A到达末尾时,使链表A指向headB的头结点,否则前移
            A = A == null ? headB : A.next;
            // 链表B到达末尾时,使链表B指向headA的头结点,否则前移
            B = B == null ? headA : B.next;
        }
        // 两个链表没有交点时,会一起到达终点,都为null
        return A;
    }
}

Time complexity: O(m+n), m is the length of linked list A, n is the length of linked list B.
Space complexity: O(1)

Guess you like

Origin blog.csdn.net/xrrrrrrrrrrr/article/details/112979066