using System.Collections.Generic;
public static void Main()
Check check = new Check();
int[] numbers = new int[]{1,2,3,4};
Node listOne = LinkedList.CreateLinkedList(numbers);
Node listTwo = LinkedList.CreateLinkedList(numbers);
Console.WriteLine("Given two lists has no intersection. Expected Answer is False. Program answer is " + check.TheyIntersect(listOne, listTwo));
Console.WriteLine(" Using Dictionary - Given two lists has no intersection. Expected Answer is False. Program answer is " + check.TheyIntersect_UsingDictionary(listOne, listTwo));
Node node1 = new Node(1,null);
Node node2 = new Node(2,node1);
Node listOneFirst = new Node(3,node2);
Node node4 = new Node(4,node1);
Node listTwoFirst = new Node(5,node4);
Console.WriteLine("Given two lists do intersect. Expected Answer is True. Program answer is " + check.TheyIntersect(listOneFirst, listTwoFirst));
Console.WriteLine("Using Dictionary - Given two lists do intersect. Expected Answer is True. Program answer is " + check.TheyIntersect_UsingDictionary(listOneFirst, listTwoFirst));
public bool TheyIntersect(Node node1, Node node2)
Node listTwoFirst = node2;
if(Object.ReferenceEquals(node1, node2))
public bool TheyIntersect_UsingDictionary(Node node1, Node node2)
Dictionary<Node,int> nodeDictionary = new Dictionary<Node,int>();
nodeDictionary.Add(node1,1);
if(nodeDictionary.ContainsKey(node2))
public static class LinkedList
public static int Length(Node first)
public static Node PadZerosOnHead(Node first, int numberOfZeros)
for(int i = 1; i<= numberOfZeros; i++)
node = AddToHead(node,0);
public static Node CreateLinkedList(int[] numbers)
Node first = new Node(numbers[0], null);
for(int i = 1; i<numbers.Length; i++)
for(int j = i - 1; j > 0 ; j--)
Node newNode = new Node(numbers[i],null);
public static Node RemoveNode(Node first, int data)
if(node.Next.Data == data)
node.Next = node.Next.Next;
public static Node AddToTail(Node first, int data)
return new Node(data,null);
Node newNode = new Node(data,null);
public static Node AddToHead(Node first, int data)
return new Node(data,null);
Node newNode = new Node(data,null);
public static void DisplayLinkedList(Node first)
String a = first.Data.ToString() + " => ";
a += node.Data.ToString() + " => ";
a = a.Remove(a.Length - 4, 4);
public Node(int data, Node next)