Find Intersection
by Isai Damier, Android Engineer @ Google

#=======================================================================
# Author: Isai Damier
# Title: Singly Linked List
# Project: geekviewpoint
# Package: datastructure
#
# Description: A LinkedList is a data structure that allows access
#   to a collection of data using pointers/references. While an
#   array can also be defined as above, LinkedLists and arrays differ
#   in how they are stored in memory and in the operations they
#   allow. Unlike an array that must be stored in a block of memory,
#   the nodes of a LinkedList can be stored anywhere because each
#   node has a reference to the node that succeeds it. Because the
#   nodes are stored so loosely, inserting nodes into a LinkedList
#   is easy; whereas in an array, all the succeeding elements must
#   be shifted. Of course, insertion also means changing the size of
#   the array, which means creating the entire array anew.
#
#   Perhaps the greatest beauty of LinkedList is that it allows
#   accessing an entire sequence of nodes using only one variable:
#   a reference to the first node in the sequence.
#
#   Countless operations can be performed on LinkedLists. Following
#   are a few, ranging from the common to the very interesting.
#=======================================================================
  #=====================================================================
  # Time Complexity of Solution:
  #   O(n).
  #
  # Description: Find the node (intersection) where the two given
  #   LinkedList coalesce.
  #
  # Technical Details: The idea is that two linked lists form a Y-shaped
  #   structure. The structure may be degenerated. As shown below, the use
  #   of a hashmap greatly reduces the complexity of the problem.
  #
  #
  #   To keep things simple, we consider t2 and t2 equal iff they both
  #   point to the same memory location. A broader definition of
  #   equality: Two nodes t2 and t1 are equal if they and their respective
  #   sublists are equal.
  #=====================================================================
 
 import collections
class SinglyLinkedList( object ):

  def __init__( self ):
    self.head , self.tail = None, None

  @classmethod
  def findIntersection( self, head1, head2 ):
    intersect = {}
    t = head1
    while None != t:
      intersect[t] = True
      t = t.next

    # first duplicate is intersection
    t = head2
    while None != t:
      if None != intersect.get( t ):
        return t
      t = t.next
    return None

class Node( object ):

  def __init__( self, data, next = None ):
    self.data = data
    self.next = next
import unittest
from algorithms.SinglyLinkedList import SinglyLinkedList
import random

class Test( unittest.TestCase ):
  #=====================================================================
  # Test of findIntersection method, of class SinglyLinkedList.
  #=====================================================================
  def testFindIntersection( self ):
    in_1 = [29, 14, 35, 2, 1, 12, 6, 7, 4, 8, 3, 0, 16, 19, 11]
    in_2 = [99, 78, 8, 3, 23]

    list_1 = SinglyLinkedList()
    list_2 = SinglyLinkedList()

    for i in range( len( in_1 ) ):
      list_1.addToTail( in_1[i] )

    self.assertEquals( len( in_1 ), list_1.size() )

    for i in range( len( in_2 ) ):
      list_2.addToTail( in_2[i] )

    self.assertEquals( len( in_2 ), list_2.size() )

    # set intersection: list_1 and list_2 form a Y at element 6
    exp = 6
    found = list_1.find( exp )
    self.assertIsNotNone( found )
    list_2.addAllToTail( found )
    self.assertEquals( 14, list_2.size() )
    n = SinglyLinkedList.findIntersection( list_1.head, list_2.head )

    self.assertEquals( exp, n.data )