Search This Blog

Wednesday, January 1, 2014

Algorithm - Linked Lists - Insert a node at a specific position in a linked list

Insert a node at a specific position in a linked list

The following is the solution to Hacker Rank problem Insert a node at a specific position in 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
Node* InsertNth(Node *head, int data, int position)
{
  // Complete this method only
  // Do not write main function.
 
    Node *newNode = new Node();
    newNode->data = data;
    newNode->next = NULL;
    Node *temp = head;
 
    if(position == 0)
    {
        newNode->next = head;
        head = newNode;
    }
    else
    {
        for(int i = 1; i < position; i++)
            temp = temp->next;
        newNode->next = temp->next;
        temp->next = newNode;
    }
    return head;
}

Algorithm - Linked Lists - Delete a node from a linked list

Delete a node from a linked list

The following is the solution to Hacker Rank problem Delete a node from 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
Node* Delete(Node *head, int position)
{
    // Complete this method
    Node *temp = head;
    if(position == 0)
        head = head->next;
    else
    {
        for(int i = 1; i < position; i++)
            temp = temp->next;
        Node *del = temp->next;
        temp->next = del->next;
    }
    return head;
}

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

Print the elements of a linked list in reverse

The following is the solution to Hacker Rank problem Print the elements of a linked list in reverse 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
void ReversePrint(Node *head)
{

  if(head == NULL)
    return;
   
  // traverse the list
  ReversePrint(head->next);

//print the element
  printf("%d\n", head->data);
}

Friday, December 6, 2013

Algorithm - Search - Find the Median

Find the Median

The following is the solution to Hacker Rank problem Find the Median using Java.  For solutions to other Hacker Rank Problem visit my page HackerRank, alternatively try searching for the problem in my blog.

Score: 10/10
/**
 *
 */


import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

/**
 * @author Arun.G
 *
 */
public class Solution{

       /**
        * @param args
        */

       public static void main(String[] args) {
              /*
               * Enter your code here. Read input from STDIN. Print output to STDOUT.
               * Your class should be named Solution.
               */
              Scanner sc = new Scanner(System.in);
              int n = sc.nextInt();
              int[] ar = new int[n];
              for (int i = 0; i < n; i++) {
                     ar[i] = sc.nextInt();
              }
              Arrays.sort(ar);

              int median = ar[ar.length / 2];
              System.out.println(median);

              sc.close();
       }

}       

Monday, November 25, 2013

Algorithm - Linked Lists - Insert a node at the tail of a linked list

Insert a node at the tail of a linked list

The following is the solution to Hacker Rank problem Insert a node at the tail 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;
};/*
  Insert Node at the end of a linked list
  head pointer input could be NULL as well for empty list
  Node is defined as
  struct Node
  {
     int data;
     struct Node *next;
  }
*/
Node* Insert(Node *head,int data)
{
  // Complete this method
    Node *headPtr = new Node();
    headPtr = head;
    if(head!=NULL)
    {
        while(head->next!=NULL)
        {
            head = head->next;
        }
        Node *tail = new Node();
         tail->data = data;
        tail->next = NULL;
        head->next = tail;               
    }
    else
    {
        Node *tail = new Node();
         tail->data = data;
        tail->next = NULL;
        headPtr = tail;
    }
       
   
    return headPtr;
}void Print(Node *head)
{
                Node *temp = head;
                while(temp!= NULL){ cout<<temp->data<<"\n";temp = temp->next;}
}
int main()
{
                int t;
                cin>>t;
                Node *head = NULL;
                while(t-- >0)
                {
                                int x; cin>>x;
                                head = Insert(head,x);
                }
                Print(head);
}

Algorithm - Linked Lists- Insert a node at the head of a linked list

Insert a node at the head of a linked list

The following is the solution to Hacker Rank problem  Insert a node at the head 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;
};/*
  Insert Node at the begining of a linked list
  Initially head pointer argument could be NULL for empty list
  Node is defined as
  struct Node
  {
     int data;
     struct Node *next;
  }
return back the pointer to the head of the linked list in the below method.
*/
Node* Insert(Node *head,int data)
{
     Node *newHead = new Node();
  // Complete this method
    if(head!= NULL)
    {     
        newHead->data = data;
        newHead->next = head;
    }
    else
    {     
        newHead->data = data;
        newHead->next = NULL;
    }
    return newHead;
}void Print(Node *head)
{
                Node *temp = head;
                while(temp!= NULL){ cout<<temp->data<<"\n";temp = temp->next;}
}
int main()
{
                int t;
                cin>>t;
                Node *head = NULL;
                while(t-- >0)
                {
                                int x; cin>>x;
                                head = Insert(head,x);
                }
                Print(head);
}

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

}

Labels