Insert a node into a sorted doubly linked list Solution


Problem Statement

You’re given the pointer to the head node of a sorted doubly linked list and an integer to insert into the list. Create a node and insert it into the appropriate position in the list. The head node might be NULL to indicate that the list is empty.

Source Code:

 Node* SortedInsert(Node *head,int data)  
 {  
   // Complete this function  
   // Do not write the main method.   
   Node *temp = new Node;  
   temp->data = data;  
   temp->next = NULL;  
   temp->prev = NULL;  
   if(head == NULL){  
     head = temp;  
   }else{  
     Node *temp1 = temp;  
     temp->next = head;  
     head = temp;  
     while(temp!= NULL){  
       temp1 = temp;  
       temp = temp->next;  
       if(temp!=NULL){  
         if(temp1->data>temp->data){  
           int tdata;  
           tdata = temp1->data;  
           temp1->data = temp->data;  
           temp->data = tdata;  
         }  
       }  
     }  
   }  
   return head;  
 }  

C++ Diamond Pattern


C++ program to print the diamond pattern using '#' symbols. In this program two for loops are used to print the pattern. The first loop prints the upper part to the diamond ( triangle part ). The second for loop prints the lower part of the diamond (an inverted triangle ) . By combining both of the triangles an diamond pattern structure is formed.

PROGRAM CODE:

 #include<iostream>  
 using namespace std;  
  int main()  
  {  
    int i,j,k=1,s=6,l;  
    for(i=0;i<5;i++,k-=2,s-=1)  
    {  
      for(l=s-1;l>=0;l--)  
      cout<<' ';  
      for(j=k;j<0;j++)  
        cout<<"#";  
      cout<<endl;  
   }  
 k = 9;  
 s = 0;  
 for(i=0;i<5;i++,k-=2,s+=1)  
    {  
      for(l=s;l>=0;l--)  
      cout<<' ';  
      for(j=k;j>0;j--)  
         cout<<"#";  
      cout<<endl;  
    }  
  return 0;  
  }  

OUTPUT:

           #
        # # #
     # # # # #
   # # # # # # #
# # # # # # # # #
   # # # # # # #
      # # # # #
        # # #
          # 

** The above solution is my own code and it may not be the optimal solution or optimal way to approach the problem. So if you have any optimal approaches feel free to paste the code as the comment below..... :) :) :)

Print the elements of a linked list solution


Problem Statement

You’re given the pointer to the head node of a linked list and you need to print all its elements in order, one element per line. The head pointer may be null, i.e., it may be an empty list. In that case, don’t print anything!

Source code:

 /*  
  Print elements of a linked list on console   
  head pointer input could be NULL as well for empty list  
  Node is defined as   
  struct Node  
  {  
    int data;  
    struct Node *next;  
  }  
 */  
 void Print(Node *head)  
 {  
  // This is a "method-only" submission.   
  // You only need to complete this method.   
   while(head!=NULL){  
     cout<<head->data<<endl;  
     head = head->next;  
   }  
 }  

Print a linked list in Reverse solution


Problem Statement

You're given the pointer to the head node of a linked list and you need to print all its elements in reverse order from tail to head, one element per line. The head pointer may be null meaning that the list is empty - in that case, don't print anything!

Soruce code:

 /*  
  Print elements of a linked list in reverse order as standard output  
  head pointer could be NULL as well for empty list  
  Node is defined as   
  struct Node  
  {  
    int data;  
    struct Node *next;  
  }  
 */  
 void ReversePrint(Node *head)  
 {  
  // This is a "method-only" submission.   
  // You only need to complete this method.   
   int a[100],i=0;  
   while(head!=NULL){  
     a[i] = head->data;  
     i++;  
     head = head->next;  
   }  
   for(int j = i-1;j>=0;j--){  
     cout<<a[j]<<endl;  
   }  
 }  

Reverse a doubly linked list solution


Problem Statement

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;  
 }  

Delete duplicate value nodes from a sorted linked list Solution


Problem Statement

You're given the pointer to the head node of a sorted linked list, where the data in the nodes is in ascending order. Delete as few nodes as possible so that the list does not contain any value more than once. The given head pointer may be null indicating that the list is empty.

Source code:

 /*  
  Remove all duplicate elements from a sorted linked list  
  Node is defined as   
  struct Node  
  {  
    int data;  
    struct Node *next;  
  }  
 */  
 Node* RemoveDuplicates(Node *head)  
 {  
  // This is a "method-only" submission.   
  // You only need to complete this method.   
   Node *cur = head;  
   while(cur->next!=NULL){  
     if(cur->data == cur->next->data)  
       cur->next = cur->next->next;   
     else  
     cur = cur->next;  
   }  
   return head;  
 }  

Utopian Tree



Problem Statement
The Utopian Tree goes through 2 cycles of growth every year. The first growth cycle occurs during the spring, when it doubles in height. The second growth cycle occurs during the summer, when its height increases by 1 meter. 
Now, a new Utopian Tree sapling is planted at the onset of spring. Its height is 1 meter. Can you find the height of the tree after N growth cycles?

SOURCE CODE:

 #include<stdio.h>  
 void main()  
 {  
   int t,test,total;  
   scanf("%d",&test);  
   while(test--){  
     scanf("%d",&t);  
     total=1;  
     int flag=1;  
     while(t--)  
     {  
       if(flag){  
         if(total==1){  
           total+=1;  
         }else{  
         total*=2;  
         }  
       flag--;  
       }  
       else{  
         total+=1;  
         flag++;  
       }  
     }  
     printf("%d\n",total);  
   }  
 }