Exemple #1
0
        public NodeLL RemoveDifference(NodeLL start, int k)
        {
            NodeLL current = start;

            for (int i = 0; i < k; i++)
            {
                current = current.next;
            }
            return(current);
        }
Exemple #2
0
        public int GetLengthofLinkedList(NodeLL start)
        {
            int count = 0;

            while (start != null)
            {
                count++;
                start = start.next;
            }
            return(count);
        }
Exemple #3
0
        public NodeLL GetMiddle(NodeLL start)
        {
            NodeLL slow = start;
            NodeLL fast = start;

            while (fast != null && fast.next != null)
            {
                fast = fast.next.next;
                slow = slow.next;
            }
            return(slow);
        }
Exemple #4
0
 public NodeLL GetCommon(NodeLL current, NodeLL start)
 {
     while (current != null && start != null)
     {
         if (current.data == start.data)
         {
             return(current);
         }
         else
         {
             current = current.next;
             start   = start.next;
         }
     }
     return(null);
 }
Exemple #5
0
        public void Remove(NodeLL start)
        {
            HashSet <int> h = new HashSet <int>();

            NodeLL prev = null;

            while (start != null)
            {
                if (!h.Contains(start.data))
                {
                    h.Add(start.data);
                    prev = start;
                }
                else
                {
                    prev.next = start.next;
                }
                start = start.next;
            }
        }
Exemple #6
0
        public void GetDifferece(NodeLL start1, NodeLL start2)
        {
            int l1   = GetLengthofLinkedList(start1);
            int l2   = GetLengthofLinkedList(start2);
            int diff = Math.Abs(l1 - l2);

            if (l1 > l2)
            {
                NodeLL current = RemoveDifference(start1, diff);
                NodeLL common  = GetCommon(current, start2);
                Console.WriteLine(common.data);
                Console.ReadKey();
            }
            else
            {
                NodeLL current = RemoveDifference(start2, diff);
                NodeLL common  = GetCommon(current, start1);
                Console.WriteLine(common.data);
                Console.ReadKey();
            }
        }
Exemple #7
0
        public void Queueusingstack(NodeLL start)
        {
            Stack <NodeLL> s1 = new Stack <NodeLL>();
            Stack <NodeLL> s2 = new Stack <NodeLL>();

            while (start != null)
            {
                s1.Push(start);
                start = start.next;
            }
            while (s1.Count != 0)
            {
                NodeLL i = s1.Pop();
                s2.Push(i);
            }
            while (s2.Count != 0)
            {
                NodeLL j = s2.Pop();
                Console.WriteLine(j.data);
            }
        }
Exemple #8
0
        public NodeLL DetectCycleNode(NodeLL start)
        {
            NodeLL slow = start;
            NodeLL fast = start;

            while (start != null)
            {
                slow = start.next;
                fast = fast.next.next;
                if (slow == fast)
                {
                    break;
                }
            }
            slow = start;
            while (slow != fast)
            {
                slow = slow.next;
                fast = fast.next;
            }
            return(fast);
        }