Saturday, 18 April 2015

Hackerrank Reverse a doubly linked list solution

Problem Statement

This challenge is part of a tutorial track by MyCodeSchool

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.

Source Code:

 /*  
   Reverse a doubly linked list, input list may also be empty  
   Node is defined as  
   struct Node  
   {  
    int data;  
    Node *next;  
    Node *prev;  
   }  
 */  
 Node* Reverse(Node* head)  
 {  
   Node *cur = head,*temp = new Node;  
   // Complete this function  
   // Do not write the main method.   
   while(cur !=NULL){  
     temp->next = cur->next;  
     temp->prev = cur->prev;  
     cur->next = temp->prev;  
     cur->prev = temp->next;  
     cur = temp->next;  
     if(cur!=NULL){  
       head = cur;  
     }  
   }  
   return head;  
 }  
** The above solution is my own code and it may not be the optimal solution or optimal way to approach the problem but it passes all the testcases in Hackerrank. So if you have any optimal approaches feel free to paste the code as the comment below..... :) :) :)

2 comments:

  1. Our writing service is dedicating the excellent writing process and programs in online. The high volume of writing is possible in our writing service. You can manage your writing papers by the experts. The learning guide to be provide for the experts to the students.The essay writing service is available in our writing service

    ReplyDelete
  2. I want to check the whole tutorial now. Will you help me with that, friend?

    ReplyDelete