Sword finger Offer 52. The first common node of the two linked lists

Keywords: Java data structure linked list

Sword finger Offer 52. The first common node of the two linked lists

Title:
Enter two linked lists and find their first common node.
As shown in the following two linked lists:

The intersection begins at node c1.

Example 1:

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 intersection node is 8 (note that if two lists intersect, it cannot be 0). Starting from their 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 intersection node; In B, there are 3 nodes before the intersection node.

Example 2:

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 intersection node is 2 (note that if two lists intersect, it cannot be 0). Starting from the respective header, 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 intersection node; In B, there is 1 node before the intersection node.

Example 3:

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

Problem solving ideas:

Method 1:
       First, traverse the linked list headA, and add each node in the linked list headA to the hash set. Then traverse the linked list headB, and judge whether the node is in the hash set for each node traversed. If yes, return to the current node; If all nodes are not present, it indicates disjoint and returns null.

Method 2:
       Using the double pointer method, the spatial complexity can be reduced to O(1). When the linked list headA and headB are not empty, create two pointers pA and pB. Initially, point to the head nodes headA and headB of the two linked lists respectively, and then traverse each node of the two linked lists in turn.

The code is as follows:

1. Method 1:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 * Execution result: passed
 * Execution time: 7 ms, beating 23.25% of users in all Java submissions
 * Memory consumption: 42.3 MB, beating 5.14% of users in all Java submissions
 * Pass test case: 39 / 39
 */
public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        Set<ListNode> visited = new HashSet<ListNode>();
        ListNode temp = headA;
        while (temp != null) {
            visited.add(temp);
            temp = temp.next;
        }
        temp = headB;
        while (temp != null) {
            if (visited.contains(temp)) {
                return temp;
            }
            temp = temp.next;
        }
        return null;
    }
}

Method 2:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 * Execution result: passed
 * Execution time: 1 ms, beating 100.00% of users in all Java submissions
 * Memory consumption: 40.6 MB, beating 98.61% of users in all Java submissions
 * Pass test case: 45 / 45
 */
public class Solution {
    public ListNode getIntersectionNode(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;
    }
}

Posted by Blockis on Mon, 25 Oct 2021 21:44:10 -0700