Tags: "leetcode", "linked-list", access_time 2-min read

Edit this post on Github

Intersection of Two Linked Lists

Created: October 14, 2018 by [lek-tin]

Last updated: October 10, 2019

Write a program to find the node at which the intersection of two singly linked lists begins.

For example, the following two linked lists:

A:         a1 → a2
                   ↘
                     c1 → c2 → c3
                   ↗
B:    b1 → b2 → b3

begin to intersect at node c1.

Note

If the two linked lists have no intersection at all, return null. The linked lists must retain their original structure after the function returns. You may assume there are no cycles anywhere in the entire linked structure. Your code should preferably run in O(n) time and use only O(1) memory. Credits: Special thanks to @stellari for adding this problem and creating all test cases.

Solution

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None
#      A:   x steps
#                   ↘
#                     common steps
#                   ↗            ↓
#           y steps ← ← ← ← ← ← ↙
#      --------------------------------
#           x steps ← ← ← ← ← ← ↖
#                   ↘            ↑
#                     common steps
#                   ↗
#      B:   y steps
# Total step for A and B are the same
class Solution(object):
    def getIntersectionNode(self, headA, headB):
        """
        :type head1, head1: ListNode
        :rtype: ListNode
        """
        if headA == None or headB == None:
            return None

        a = headA
        b = headB

        while a != b:
            if a != None and b != None:
                print(a.val, b.val)
            a = a.next if a != None else headB
            b = b.next if b != None else headA

        return a
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if (headA == null || headB == null) {
            return null;
        }
        ListNode a = headA;
        ListNode b = headB;

        while (a != b) {
            if (a != null) {
                a = a.next;
            } else {
                a = headB;
            }

            if (b != null) {
                b = b.next;
            } else {
                b = headA;
            }
        }

        return a;
    }
}