Advertisement
Guest User

Untitled

a guest
May 13th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.30 KB | None | 0 0
  1. using System;
  2.  
  3. class Program
  4. {
  5.     static void Main(string[] args)
  6.     {
  7.         var problem = new Problem();
  8.         problem.PrepareTestCase1();
  9.         problem.Solve();
  10.  
  11.         problem = new Problem();
  12.         problem.PrepareTestCase2();
  13.         problem.Solve();
  14.  
  15.         problem = new Problem();
  16.         problem.PrepareTestCase3();
  17.         problem.Solve();
  18.  
  19.         Console.WriteLine("Press any key...");
  20.         Console.ReadKey(true);
  21.     }
  22. }
  23.  
  24. public class ListNode
  25. {
  26.     public int Value;
  27.     public ListNode Next;
  28.     public ListNode(int value)
  29.     {
  30.         Value = value;
  31.     }
  32. }
  33.  
  34. public class LinkedList
  35. {
  36.     public int Length = 0;
  37.     public ListNode Head = null, Tail = null;
  38.  
  39.     public void Add(int value)
  40.     {
  41.         if (Head == null)
  42.         {
  43.             Head = new ListNode(value);
  44.             Tail = Head;
  45.         }
  46.         else
  47.         {
  48.             Tail.Next = new ListNode(value);
  49.             Tail = Tail.Next;
  50.         }
  51.         Length++;
  52.     }
  53.  
  54.     public void Add(LinkedList list)
  55.     {
  56.         if (Head == null)
  57.         {
  58.             Head = list.Head;
  59.             Tail = Head;
  60.         }
  61.         else
  62.         {
  63.             Tail.Next = list.Head;
  64.             Tail = list.Tail;
  65.         }
  66.         Length += list.Length;
  67.     }
  68. }
  69.  
  70. public class Problem
  71. {
  72.     private LinkedList listA = new LinkedList(), listB = new LinkedList();
  73.  
  74.     private ListNode getIntersectionNode()
  75.     {
  76.         if (listA == null || listB == null)
  77.             return null;
  78.  
  79.         if (listA.Tail != listB.Tail)
  80.             return null; // both list must end with the same node if they intersect with each other
  81.  
  82.         var diff = Math.Abs(listA.Length - listB.Length);
  83.         if (listA.Length > listB.Length)
  84.             while (diff-- > 0)
  85.                 listA.Head = listA.Head.Next; // skip listA's head if listA has more elements than listB
  86.         else
  87.             while (diff-- > 0)
  88.                 listB.Head = listB.Head.Next; // skip listB's head if listB has more elements than listA
  89.  
  90.         while (listA.Head != listB.Head) // while not intersect
  91.         {
  92.             listA.Head = listA.Head.Next;
  93.             listB.Head = listB.Head.Next;
  94.         }
  95.  
  96.         return listA.Head;
  97.     }
  98.  
  99.     public void Solve()
  100.     {
  101.         var intersectionNode = getIntersectionNode();
  102.         Console.WriteLine("intersection = {0}", intersectionNode != null ? intersectionNode.Value.ToString() : "N/A");
  103.     }
  104.  
  105.     public void PrepareTestCase1()
  106.     {
  107.         var intersection = new LinkedList();
  108.         intersection.Add(8);
  109.         intersection.Add(4);
  110.         intersection.Add(5);
  111.  
  112.         listA.Add(4);
  113.         listA.Add(1);
  114.         listA.Add(intersection);
  115.  
  116.         listB.Add(5);
  117.         listB.Add(0);
  118.         listB.Add(1);
  119.         listB.Add(intersection);
  120.     }
  121.  
  122.     public void PrepareTestCase2()
  123.     {
  124.         var intersection = new LinkedList();
  125.         intersection.Add(2);
  126.         intersection.Add(4);
  127.  
  128.         listA.Add(0);
  129.         listA.Add(9);
  130.         listA.Add(1);
  131.         listA.Add(intersection);
  132.  
  133.         listB.Add(3);
  134.         listB.Add(intersection);
  135.     }
  136.  
  137.     public void PrepareTestCase3()
  138.     {
  139.         listA.Add(2);
  140.         listA.Add(6);
  141.         listA.Add(4);
  142.  
  143.         listB.Add(1);
  144.         listB.Add(5);
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement