Ejemplo n.º 1
0
        private void DisplayLinkedList(MyLinkedListNode node)
        {
            Console.WriteLine();
            MyLinkedListNode current = node;

            if (node == null)
            {
                Console.WriteLine("This is an empty list");
            }
            while (current != null)
            {
                Console.Write("{0}\t", current.Data);
                current = current.Next;
            }
            Console.WriteLine();
        }
Ejemplo n.º 2
0
 private MyLinkedListNode AddTwoNosRightToLeft(MyLinkedListNode node1, MyLinkedListNode node2, ref int carry)
 {
     if (node1 == null && node2 == null)
     {
         return(null);
     }
     else
     {
         MyLinkedListNode nextNode = AddTwoNosRightToLeft(node1, node2, ref carry);
         int sum = node1.Data + node2.Data + carry;
         carry = sum / 10;
         MyLinkedListNode node = new MyLinkedListNode(sum % 10);
         node.Next = nextNode;
         return(node);
     }
 }
Ejemplo n.º 3
0
        private bool MakeCycle(MyLinkedList linkedList)
        {
            MyLinkedListNode firstNode   = linkedList.Head;
            MyLinkedListNode currentNode = linkedList.Head;

            if (firstNode == null || firstNode.Next == null)
            {
                return(false);
            }
            while (currentNode.Next != null)
            {
                currentNode = currentNode.Next;
            }
            currentNode.Next = firstNode;
            return(true);
        }
Ejemplo n.º 4
0
        int DeQueue()
        {
            if (head == null)
            {
                Console.WriteLine("There are no items in the queues");
                return(-1);
            }
            int data = head.Data;

            if (head.Next == null)
            {
                tail = null;
            }
            head = head.Next;
            return(data);
        }
Ejemplo n.º 5
0
        public MyLinkedList AddTwoNosLeftToRight(MyLinkedList list2)
        {
            MyLinkedList list = new MyLinkedList();

            MyLinkedListNode node1 = this.Head;
            MyLinkedListNode node2 = list2.Head;
            MyLinkedListNode node  = null;
            MyLinkedListNode head  = list.Head;
            int carry = 0;

            while (node1 != null || node2 != null)
            {
                int no1 = node1 != null ? node1.Data : 0;
                int no2 = node2 != null ? node2.Data : 0;

                int sum = no1 + no2 + carry;
                int n   = sum % 10;
                carry = sum / 10;

                if (list.Head == null)
                {
                    node      = new MyLinkedListNode(n);
                    list.Head = node;
                }
                else
                {
                    node.Next = new MyLinkedListNode(n);
                    node      = node.Next;
                }
                if (node1 != null)
                {
                    node1 = node1.Next;
                }
                if (node2 != null)
                {
                    node2 = node2.Next;
                }
            }

            if (carry > 0)
            {
                node.Next = new MyLinkedListNode(carry);
            }

            return(list);
        }
Ejemplo n.º 6
0
        public MyLinkedListNode Append(int data)
        {
            MyLinkedListNode newNode = new MyLinkedListNode(data);

            if (Head == null)
            {
                Head = newNode;
                return(Head);
            }
            MyLinkedListNode current = Head;

            while (current.Next != null)
            {
                current = current.Next;
            }
            current.Next = newNode;
            return(Head);
        }
Ejemplo n.º 7
0
        private void TestMergeLinkedList()
        {
            Console.WriteLine("Preparing linked list");
            List <int> dataPoints = new List <int>()
            {
                1, 3, 7, 9, 13
            };
            MyLinkedList linkedList1 = CreateLinkedList(dataPoints);

            dataPoints = new List <int>()
            {
                2, 4, 8
            };
            MyLinkedList     linkedList2 = CreateLinkedList(dataPoints);
            MyLinkedListNode result      = MergeLists1(linkedList1.Head, linkedList2.Head);

            Console.WriteLine("Displaying Linked List");
            DisplayLinkedList(result);
            Console.ReadLine();
        }
Ejemplo n.º 8
0
        private void TestLinkedList()
        {
            Console.WriteLine("Preparing linked list");
            List <int> dataPoints = new List <int>()
            {
                3, 2, 10, 3, 5
            };
            MyLinkedListNode node       = null;
            MyLinkedList     linkedList = CreateLinkedList(dataPoints);

            Console.WriteLine("Displaying Linked List");
            DisplayLinkedList(linkedList.Head);

            //MakeCycle(linkedList);
            Console.WriteLine("Detecting any cycle");
            string message = "The Linked list " + (HasCycles(linkedList) ? " is cyclic" : "is not cyclic");

            Console.WriteLine(message);
            Console.ReadLine();
        }
Ejemplo n.º 9
0
        //Used method of two pointers with different increment count.
        //Note here the first Node increments by one.Second one increments by two.
        //Think of two cars with varying speeds eventually will pass through each other in race track.
        //Refer HackerRank video on cycle in LinkedList
        private bool HasCycles(MyLinkedList linkedList)
        {
            MyLinkedListNode node1 = linkedList.Head;

            if (node1 == null || node1.Next == null)
            {
                return(false);
            }
            MyLinkedListNode node2 = linkedList.Head.Next;

            while (node1 != null && node2 != null && node2.Next != null)
            {
                if (node1 == node2)
                {
                    return(true);
                }
                node1 = node1.Next;
                node2 = node2.Next.Next;
            }
            return(false);
        }
Ejemplo n.º 10
0
        public MyLinkedListNode Delete(MyLinkedListNode head, int position)
        {
            int deleteAtIndex        = 0;
            MyLinkedListNode current = head;
            MyLinkedListNode prev    = null;

            while (deleteAtIndex < position)
            {
                prev    = current;
                current = current.Next;
                deleteAtIndex++;
            }
            if (position == 0)
            {
                head = head.Next;
            }
            else
            {
                prev.Next = current.Next;
            }
            return(head);
        }
Ejemplo n.º 11
0
        internal void DeleteWithValue(int data)
        {
            if (Head == null)
            {
                return;
            }
            if (Head.Data == data)
            {
                Head = Head.Next;
                return;
            }
            MyLinkedListNode current = Head;

            while (current.Next != null)
            {
                if (current.Next.Data == data)
                {
                    current.Next = current.Next.Next;
                    break;
                }
                current = current.Next;
            }
        }
Ejemplo n.º 12
0
        /*
         * Given pointers to the head nodes of  linked lists that merge together at some point,
         * find the Node where the two lists merge. It is guaranteed that the two head
         * Nodes will be different, and neither will be NULL.
         *
         * In the diagram below, the two lists converge at Node x:
         *
         * [List #1] a--->b--->c
         \
         \            x--->y--->z--->NULL
         \           /
         \ [List #2] p--->q
         \ Complete the int FindMergeNode(Node* headA, Node* headB) method so that it finds
         \ and returns the data value of the Node where the two lists merge.
         *
         */

        private int FindMergeNode(MyLinkedListNode headA, MyLinkedListNode headB)
        {
            int result = 0;

            bool mergePointFound = false;

            while (headA != null && !mergePointFound)
            {
                MyLinkedListNode currentB = headB;
                while (currentB != null && !mergePointFound)
                {
                    if (headA == currentB)
                    {
                        mergePointFound = true;
                        result          = headA.Data;
                        break;
                    }
                    currentB = currentB.Next;
                }
                currentB = headB;
                headA    = headA.Next;
            }
            return(result);
        }
Ejemplo n.º 13
0
        public static int GetNode(MyLinkedListNode head, int positionFromTail)
        {
            int counter = -1;

            return(FindNode(head, ref positionFromTail, ref counter));
        }
Ejemplo n.º 14
0
        public static MyLinkedListNode MergeLists1(MyLinkedListNode headA, MyLinkedListNode headB)
        {
            MyLinkedListNode resultCurrent = null;
            MyLinkedListNode resultHead    = null;

            while (headA != null && headB != null)
            {
                MyLinkedListNode newNode = null;
                if (headA.Data < headB.Data)
                {
                    newNode = new MyLinkedListNode(headA.Data);
                    headA   = headA.Next;
                }
                else
                {
                    newNode = new MyLinkedListNode(headB.Data);
                    headB   = headB.Next;
                }
                if (resultHead == null)
                {
                    resultHead    = newNode;
                    resultCurrent = newNode;
                }
                else
                {
                    resultCurrent.Next = newNode;
                    resultCurrent      = resultCurrent.Next;
                }
            }

            while (headA != null)
            {
                MyLinkedListNode newNode = new MyLinkedListNode(headA.Data);
                if (resultHead == null)
                {
                    resultHead    = newNode;
                    resultCurrent = newNode;
                }
                else
                {
                    resultCurrent.Next = newNode;
                    resultCurrent      = resultCurrent.Next;
                }
                headA = headA.Next;
            }

            while (headB != null)
            {
                MyLinkedListNode newNode = new MyLinkedListNode(headB.Data);
                if (resultHead == null)
                {
                    resultHead    = newNode;
                    resultCurrent = newNode;
                }
                else
                {
                    resultCurrent.Next = newNode;
                    resultCurrent      = resultCurrent.Next;
                }
                headB = headB.Next;
            }

            return(resultHead);
        }
Ejemplo n.º 15
0
        public static MyLinkedListNode MergeLists(MyLinkedListNode headA, MyLinkedListNode headB)
        {
            MyLinkedListNode resultCurrent = null;
            MyLinkedListNode resultHead    = null;

            while (headA != null && headB != null)
            {
                if (headA.Data < headB.Data)
                {
                    if (resultCurrent == null)
                    {
                        resultCurrent = headA;
                        resultHead    = headA;
                    }
                    else
                    {
                        resultCurrent.Next = headA;
                        resultCurrent      = resultCurrent.Next;
                    }
                    headA = headA.Next;
                }
                else
                {
                    if (resultCurrent == null)
                    {
                        resultCurrent = headB;
                        resultHead    = headB;
                    }
                    else
                    {
                        resultCurrent.Next = headB;
                        resultCurrent      = resultCurrent.Next;
                    }
                    headB = headB.Next;
                }
            }

            while (headA != null)
            {
                if (resultCurrent == null)
                {
                    resultCurrent = headA;
                    resultHead    = headA;
                }
                else
                {
                    resultCurrent.Next = headA;
                    resultCurrent      = resultCurrent.Next;
                }
                headA = headA.Next;
            }

            while (headB != null)
            {
                if (resultCurrent == null)
                {
                    resultCurrent = headB;
                    resultHead    = headB;
                }
                else
                {
                    resultCurrent.Next = headB;
                    resultCurrent      = resultCurrent.Next;
                }
                headB = headB.Next;
            }

            return(resultHead);
        }
Ejemplo n.º 16
0
 private void PropogateCarry(MyLinkedListNode head, MyLinkedListNode startNode, MyLinkedListNode resultHead, ref int carry)
 {
     if (head != startNode)
     {
         PropogateCarry(head.Next, startNode, resultHead, ref carry);
         int sum = carry = head.Data;
         MyLinkedListNode newNode = new MyLinkedListNode(sum % 10);
         carry        = sum / 10;
         newNode.Next = resultHead;
         resultHead   = newNode;
     }
 }