//here we will update current LinkedList
        //Runtime is O(n)
        public static void Linked_List_Partitioning_V2(CustomelinkedList <int> l_list, int value)
        {
            Node <int> newHead = l_list.head, newTail = l_list.head;

            int compare;

            while (l_list.head != null)
            {
                Node <int> next = l_list.head.Next;
                compare = l_list.head.data.CompareTo(value);
                if (compare == -1)
                {
                    l_list.head.Next = newHead;
                    newHead          = l_list.head;
                }
                else
                {
                    newTail.Next = l_list.head;
                    newTail      = l_list.head;
                }
                l_list.head = next;
            }
            newTail.Next = null;
            l_list.head  = newHead;
            l_list.Last  = newTail;
        }
        }// end of class



        static void Main_1(string[] args)
        {
            CustomelinkedList <int> l_list = new CustomelinkedList <int>();

            l_list.add(5);
            l_list.add(6);
            l_list.add(7);
            l_list.add(7);
            l_list.add(8);
            l_list.add(9);
            l_list.add(5);
            l_list.add(10);
            l_list.add(11);
            System.Console.WriteLine(l_list.ToString());
            System.Console.WriteLine("*****************");
            // l_list.Remve_duplication_V1();
            l_list.Remve_duplication_V2();
            System.Console.WriteLine(l_list.ToString());


            /* #resion O(n^2) implementation V1 */
            // Console.WriteLine(IsUninqe_V1(non_unique));
            // Console.WriteLine(IsUninqe_V1(unique));
            /* #endregion */

            System.Console.WriteLine("**********************");
        }
        }// end of class



        static void Main_3(string[] args)
        {
            CustomelinkedList <int> l_list = new CustomelinkedList <int>();

            l_list.add(5);
            l_list.add(6);
            l_list.add(7);
            l_list.add(8);
            l_list.add(9);
            l_list.add(10);
            // l_list.add(11);
            System.Console.WriteLine("*****************");
            System.Console.WriteLine(l_list.ToString());
            l_list.Delete_Middel_element_V1();
            System.Console.WriteLine(l_list.ToString());
            System.Console.WriteLine("**********************");
        }
        }// end of class


        //here we will use another LinkedList
        //Runtime is O(n)
        public static CustomelinkedList <int> Linked_List_Partitioning_V1(CustomelinkedList <int> l_list, int value)
        {
            CustomelinkedList <int> newLList = new CustomelinkedList <int>();
            Node <int> left = null, right = null, pointer = l_list.head;

            while (pointer != null)
            {
                Node <int> newNode = new Node <int>()
                {
                    data = pointer.data
                };
                int compare = pointer.data.CompareTo(value);
                if (compare == 0 || compare == 1)
                {
                    if (right == null)
                    {
                        right         = newNode;
                        newLList.Last = right;
                    }
                    else
                    {
                        right.Next = newNode;
                        right      = newNode;
                    }
                }
                else
                {
                    if (left == null)
                    {
                        left          = newNode;
                        newLList.head = newNode;
                    }
                    else
                    {
                        left.Next = newNode;
                        left      = newNode;
                    }
                }
                pointer = pointer.Next;
            }
            left.Next     = newLList.Last;
            newLList.Last = right;
            return(newLList);
        }
        static void Main_4(string[] args)
        {
            CustomelinkedList <int> l_list = new CustomelinkedList <int>();

            l_list.add(3);
            l_list.add(5);
            l_list.add(8);
            l_list.add(5);
            l_list.add(10);
            l_list.add(2);
            l_list.add(1);
            System.Console.WriteLine("*****************");
            System.Console.WriteLine(l_list.ToString());
            // CustomelinkedList<int> newOne =  Linked_List_Partitioning_V1(l_list,5);
            // System.Console.WriteLine(newOne.ToString());
            Linked_List_Partitioning_V2(l_list, 5);
            System.Console.WriteLine(l_list.ToString());
            System.Console.WriteLine("**********************");
        }
示例#6
0
        }// end of class



        static void Main_2(string[] args)
        {
            CustomelinkedList <int> l_list = new CustomelinkedList <int>();

            l_list.add(5);
            l_list.add(6);
            l_list.add(7);
            l_list.add(8);
            l_list.add(9);
            l_list.add(10);
            l_list.add(11);
            System.Console.WriteLine("*****************");
            int output;

            // l_list.Return_Kth_element_V1(2,out output);
            // l_list.Return_Kth_element_V2(3,out output);
            l_list.Return_Kth_element_V3(0, out output);
            System.Console.WriteLine(output);
            System.Console.WriteLine("**********************");
        }