Advertisement
Guest User

Untitled

a guest
May 13th, 2019
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.52 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class Program
  6. {
  7.     static void Main(string[] args)
  8.     {
  9.         var listA = new ListNode(new[] { 4, 5, 8, 4, 5 });
  10.         var listB = new ListNode(new[] { 5, 0, 1, 8, 4, 5 });
  11.  
  12.         //var listA = new ListNode(new[] { 0, 9, 1, 2, 4 });
  13.         //var listB = new ListNode(new[] { 3, 2, 4 });
  14.  
  15.         //var listA = new ListNode(new[] { 2, 6, 4 });
  16.         //var listB = new ListNode(new[] { 1, 5 });
  17.  
  18.         var result = new Solution().GetIntersectionNode(listA, listB);
  19.         Console.WriteLine(result != null ? result.val.ToString() : "no result");
  20.  
  21.         Console.WriteLine("Press any key...");
  22.         Console.ReadKey(true);
  23.     }
  24. }
  25.  
  26.  
  27. public class ListNode
  28. {
  29.     public int val;
  30.     public ListNode next;
  31.     public ListNode(int x)
  32.     {
  33.         val = x;
  34.     }
  35.  
  36.     public ListNode(int[] values)
  37.     {
  38.         int i = 0;
  39.         ListNode current = null;
  40.         while (i < values.Length)
  41.         {
  42.             if (i == 0)
  43.             {
  44.                 current = this;
  45.                 current.val = values[i++];
  46.             }
  47.             else
  48.             {
  49.                 current.next = new ListNode(values[i++]);
  50.                 current = current.next;
  51.             }
  52.         }
  53.     }
  54.  
  55.     public int GetLength()
  56.     {
  57.         var length = 0;
  58.         var current = this;
  59.         while (current != null)
  60.         {
  61.             length++;
  62.             current = current.next;
  63.         }
  64.         return length;
  65.     }
  66.  
  67.     public int[] ToArray()
  68.     {
  69.         var list = new List<int>();
  70.         var current = this;
  71.         while (current != null)
  72.         {
  73.             list.Add(current.val);
  74.             current = current.next;
  75.         }
  76.         return list.ToArray();
  77.     }
  78. }
  79.  
  80. public class Solution
  81. {
  82.     public ListNode ListA, ListB;
  83.  
  84.     public ListNode GetIntersectionNode(ListNode headA, ListNode headB)
  85.     {
  86.         var lengthA = headA.GetLength();
  87.         var lengthB = headB.GetLength();
  88.         int[] a, b;
  89.  
  90.         var currentA = headA;
  91.         for (var i = 0; i < lengthA; i++)
  92.         {
  93.             if (i > 0) currentA = currentA.next;
  94.             a = currentA.ToArray();
  95.  
  96.             var currentB = headB;
  97.             for (var j = 0; j < lengthB; j++)
  98.             {
  99.                 if (j > 0) currentB = currentB.next;
  100.                 b = currentB.ToArray();
  101.  
  102.                 if (a.SequenceEqual(b))
  103.                     return currentB;
  104.             }
  105.         }
  106.         return null;
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement