160. Intersection of Two Linked Lists

 

Finding the Intersection Node in Two Linked Lists with JavaScript

Finding the intersection node between two singly-linked lists is a classic problem that can be efficiently solved with a bit of clever maneuvering. In this blog post, we'll walk through the intuition, approach, and complexity analysis of finding the intersection node using JavaScript. We'll also include the complete code and a link to my detailed solution on LeetCode.

Intuition

The essence of solving this problem lies in the fact that the intersection point, if it exists, will be at the same distance from the end of both lists once they are aligned correctly. By calculating the lengths of both lists and aligning their starting points, we can simultaneously traverse the lists to find the intersection node.

Approach

  1. Calculate the Lengths of Both Lists:

    • First, we traverse both linked lists to determine their lengths. This helps in identifying the longer list and the difference in lengths between the two lists.

  2. Align the Starting Points:

    • Reset the pointers to the head of each list.

    • Advance the pointer of the longer list by the difference in lengths to align both lists for simultaneous traversal.

  3. Find the Intersection:

    • Traverse both lists simultaneously. If the nodes at the current positions are the same, we have found the intersection node.

    • If the nodes are different, continue moving both pointers one step at a time until they either meet or reach the end of the lists.

Code

Here is the complete JavaScript code to find the intersection node in two singly-linked lists:

javascript
/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */

/**
 * @param {ListNode} headA
 * @param {ListNode} headB
 * @return {ListNode}
 */
var getIntersectionNode = function(headA, headB) {
    let tempA = headA;
    let tempB = headB;
    // Calculate length
    let lengthA = 0;
    let lengthB = 0;
    while(tempA) {
        lengthA++;
        tempA = tempA.next;
    }
    while(tempB) {
        lengthB++;
        tempB = tempB.next;
    }
    // Move towards lengthA - lengthB
    tempA = headA;
    tempB = headB;
    if(lengthA > lengthB) {
        for(let i = 0; i < lengthA - lengthB; i++) {
            tempA = tempA.next;
        }
    } else {
        for(let i = 0; i < lengthB - lengthA; i++) {
            tempB = tempB.next;
        }
    }
    // Now check for intersection
    while(tempA != tempB) {
        tempA = tempA.next;
        tempB = tempB.next;
    }
    return tempA;
};

Complexity

  • Time Complexity:

    • Calculating the lengths of the two lists takes O(m + n) time, where m is the length of list A and n is the length of list B.

    • Aligning the starting points takes O(abs(m - n)) time.

    • Traversing both lists to find the intersection node takes O(min(m, n)) time.

    • Therefore, the overall time complexity is O(m + n).

  • Space Complexity:

    • The space complexity is O(1) as we only use a constant amount of extra space for pointers and counters.

Conclusion

This method provides an efficient and straightforward solution to finding the intersection node of two singly-linked lists. By aligning the lists and traversing them simultaneously, we can identify the intersection point with minimal extra space.

For more detailed explanations and additional solutions, check out my solution on LeetCode: Intersection of Two Linked Lists - LeetCode

I hope this guide helps you understand how to solve the intersection node problem using JavaScript. Happy coding! šŸš€

Comments

Popular posts from this blog

3Sum Closest: O(n²) Two-Pointer Magic šŸš€ leetcode: 16

Kadane's Algorithm: Maximum Subarray Problem - LeetCode(53) Solution

14. Longest Common Prefix