示例#1
0
        /// <summary>
        /// You’re given the pointer to the head node of a doubly linked list. Reverse the
        /// order of the nodes in the list. The head node might be NULL to indicate that the
        /// list is empty. Change the next and prev pointers of all the nodes so that the
        /// direction of the list is reversed. Return a reference to the head node of the
        /// reversed list.
        /// </summary>
        /// <param name="head"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        static DoublyLinkedListNode Solve(DoublyLinkedListNode head)
        {
            DoublyLinkedListNode reverseHead = new DoublyLinkedListNode(0), prevNode;

            if (head != null)
            {
                reverseHead.data = head.data;
                head             = head.next;
                while (head != null)
                {
                    prevNode         = reverseHead;
                    reverseHead      = head;
                    head             = head.next;
                    reverseHead.next = prevNode;
                    prevNode.prev    = reverseHead;
                }
            }
            return(reverseHead);
        }
示例#2
0
        /// <summary>
        /// Given a reference to the head of a doubly-linked list and an integer, data,
        /// create a new DoublyLinkedListNode object having data value data and insert it
        /// into a sorted linked list while maintaining the sort.
        /// </summary>
        /// <param name="head"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        static DoublyLinkedListNode Solve(DoublyLinkedListNode head, int data)
        {
            DoublyLinkedListNode newNode = new DoublyLinkedListNode(data), traverse = head;
            bool first = true;

            if (head == null)
            {
                head = newNode;
            }
            else
            {
                //Not the pretiest thing I've ever seen but hey. It works and is linear.
                while (traverse != null && newNode.prev == null && newNode.next == null)
                {
                    if (traverse.data >= newNode.data)
                    {
                        newNode.prev = traverse.prev;
                        if (!first)
                        {
                            traverse.prev.next = newNode;
                        }
                        newNode.next  = traverse;
                        traverse.prev = newNode;

                        head = first ? newNode : head;
                    }
                    else if (traverse.next == null)
                    {
                        traverse.next = newNode;
                        newNode.prev  = traverse;
                    }
                    traverse = traverse.next;
                    first    = false;
                }
            }

            return(head);
        }
示例#3
0
        public override void CompleteChallenge()
        {
            using StreamReader reader = new StreamReader(@"ReverseDLList.txt");

            int t = Convert.ToInt32(reader.ReadLine());

            for (int tItr = 0; tItr < t; tItr++)
            {
                DoublyLinkedList llist = new DoublyLinkedList();

                int llistCount = Convert.ToInt32(reader.ReadLine());

                for (int i = 0; i < llistCount; i++)
                {
                    int llistItem = Convert.ToInt32(reader.ReadLine());
                    llist.InsertNode(llistItem);
                }

                DoublyLinkedListNode llist1 = Solve(llist.head);

                PrintDoublyLinkedList(llist1, " ");
                Console.WriteLine();
            }
        }
        static void Function20()
        {
            int t = Convert.ToInt32(Console.ReadLine());

            for (int tItr = 0; tItr < t; tItr++)
            {
                DoublyLinkedList llist = new DoublyLinkedList();

                int llistCount = Convert.ToInt32(Console.ReadLine());

                for (int i = 0; i < llistCount; i++)
                {
                    int llistItem = Convert.ToInt32(Console.ReadLine());
                    llist.InsertNode(llistItem);
                }

                int data = Convert.ToInt32(Console.ReadLine());

                DoublyLinkedListNode llist1 = sortedInsert(llist.head, data);

                PrintDoublyLinkedList(llist1, " ");
                Console.WriteLine();
            }
        }
示例#5
0
 public DoublyLinkedList()
 {
     this.head = null;
     this.tail = null;
 }
示例#6
0
 public DoublyLinkedListNode(int nodeData)
 {
     this.data = nodeData;
     this.next = null;
     this.prev = null;
 }