Example #1
0
        public static MyLinkedList <char> Sum(MyLinkedList <char> list1, MyLinkedList <char> list2)
        {
            /*
             * You have two numbers represented by a linked list, where each node
             * contains a single digit. The digits are stored in reverse order, such that the 1's
             * digit is at the head of the list. Write a function that adds the two numbers
             * and returns the sum as a linked list.
             *
             * Example:
             * Input: (7->1->6) + (5->9->2). That is, 617+295.
             * Output: 2->1->9. That is, 912.
             */

            MyLinkedList <char> result;

            StringBuilder n1 = new StringBuilder();
            StringBuilder n2 = new StringBuilder();
            string        sum;

            int int1, int2, size;

            size = list1.Size > list2.Size ? list1.Size : list2.Size;

            for (int i = size; i >= 1; i--)
            {
                if (!ReferenceEquals(list1[i], null))
                {
                    n1.Append(list1[i]);
                }

                if (!ReferenceEquals(list2[i], null))
                {
                    n2.Append(list2[i]);
                }
            }

            int1 = int.Parse(n1.ToString());
            int2 = int.Parse(n2.ToString());

            sum = (int1 + int2).ToString();

            result = new MyLinkedList <char>();

            for (int i = sum.Length - 1; i >= 0; i--)
            {
                result.AddNode(sum[i]);
            }

            return(result);
        }
Example #2
0
        public static int KthToLast(int count, MyLinkedList <int> list)
        {
            /*
             * Implement an algorithm to find the Kth element from the last element
             * of a singly linked list.
             */

            int data = default(int);

            if (list.Size - count >= 1)
            {
                data = list.GetData(list.Size - count);
            }

            return(data);
        }
Example #3
0
 public static void RemoveDuplicates(MyLinkedList <int> list)
 {
     /*
      * Write code to remove duplicates from an unsorted linked list;
      */
     if (list.Size != 1)
     {
         for (int i = 1; i <= list.Size; i++)
         {
             for (int k = i + 1; k <= list.Size; k++)
             {
                 if (list.GetData(i) == list.GetData(k))
                 {
                     list.RemoveNode(k);
                     k--;
                 }
             }
         }
     }
 }
Example #4
0
        public bool CompareList(MyLinkedList <T> list)
        {
            bool equals = true;

            if (list.Size != size)
            {
                equals = false;
            }
            else
            {
                for (int i = 1; i <= Size; i++)
                {
                    if (list[i].CompareTo(this[i]) != 0)
                    {
                        equals = false;
                        break;
                    }
                }
            }

            return(equals);
        }
Example #5
0
        public static bool IsPalindrome(MyLinkedList <char> list)
        {
            /*
             * Implement a function to check if a linkedlist is palindrome.
             */

            bool p      = true;
            bool uneven = list.Size % 2 == 1;

            if (list.Size != 2)
            {
                for (int i = 1, k = list.Size; i <= list.Size / 2; i++, k--)
                {
                    if (list[i].CompareTo(list[k]) != 0)
                    {
                        p = false;
                        break;
                    }
                }
            }

            return(p);
        }