Exemplo n.º 1
0
        // link - https://www.hackerrank.com/challenges/reverse-a-doubly-linked-list/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=linked-lists
        private static void Main(string[] args)
        {
            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);
                }

                DoublyLinkedListNode llist1 = reverse(llist.head);

                PrintDoublyLinkedList(llist1, " ");
            }
        }
Exemplo n.º 2
0
        private static DoublyLinkedListNode reverse(DoublyLinkedListNode head)
        {
            if (head == null || head.next == null)
            {
                return(head);
            }

            // ONLINE SOLUTION WITHOUT USING EXTRA SPACE
            var cur  = head;
            var prev = head.prev;

            while (cur != null)
            {
                var next = cur.next;
                cur.next = prev;
                prev     = cur;
                cur      = next;
            }
            return(prev);

            // MY SOLUTION - NEW LINKED LIST
            //var newDLL = new DoublyLinkedList();

            //// get last node
            //var cur = head;
            //while (cur.next != null)
            //    cur = cur.next;

            //while (cur != null)
            //{
            //    newDLL.InsertNode(cur.data);
            //    cur = cur.prev;
            //}

            //return newDLL.head;
        }
Exemplo n.º 3
0
 public DoublyLinkedListNode(int nodeData)
 {
     this.data = nodeData;
     this.next = null;
     this.prev = null;
 }
Exemplo n.º 4
0
 public DoublyLinkedList()
 {
     this.head = null;
     this.tail = null;
 }