Search This Blog

Monday, November 25, 2013

Algorithm - Linked Lists- Print the elements of a linked list

Print the elements of a linked list
The following is the solution to Hacker Rank problem Print the elements of a linked list using Java.  For solutions to other Hacker Rank Problem visit my page HackerRank, alternatively try searching for the problem in my blog.

Score: 5/5
#include <iostream>
#include <cstdio>
#include <cstdlib>

using namespace std;

struct Node
{
                int data;
                Node *next;
};/*
  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.
     if(head != NULL)
     {
         std::cout<<head->data<<"\n";
   if(head->next != NULL)
        Print(head->next);
     }
   
}Node* Insert(Node *head,int x)
{
   Node *temp = new Node();
   temp->data = x;
   temp->next = NULL;
   if(head == NULL)
   {
       return temp;
   }
   Node *temp1;
   for(temp1 = head;temp1->next!=NULL;temp1= temp1->next);
   temp1->next = temp;return head;
}
int main()
{
                int t;
                cin>>t;
                while(t-- >0)
                {
              
                                int x; cin>>x;
                                 Node *head = NULL;
                while(x--)
                {
                                                                                 
                    int y;cin>>y;
                                           head = Insert(head,y);
                }
                Print(head);
                                                free(head);
                }

}

No comments:

Post a Comment

Labels