Monday, 12 December 2016

Reverse A Linked List Hacker Rank Solution In C++

Problem:- reverse a linked list hacker rank solution or Reverse a linked list Discussion | Data Structures or Reverse a linked list - Hacker Rank Solution or Reverse a linked list Hackerrank problem solution or reverse a linked list hacker rank solution c++ or hacker rank linked list solutions or compare two linked lists hacker rank solution or hacker rank linked list solutions or reverse a linked list in c hacker rank or reverse a linked list java or reverse a linked list c++ or hacker rank reverse list or reverse a linked list python



Explanation:- Before solving this problem (reverse a linked list) first see how linked list work for better understand this problem I have created a Full Code of Reverse Linked List so you can understand very well First check the full code and test. After that see the hacker rank solution for reverse a linked list function or segment, paste that colored code in the editor for a successfully compiled solution.

Check:- Geeksforgeeks solution for School, Basic, Easy, Medium, Hard in C++.



Submit Your Solution Here:- Click Here

Solution:-

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<stack>
using namespace std;

struct node
{
  int data;
  struct node *next;
}*root=NULL;

void createnode()
{
  int x;
  struct node *temp=(struct node*)malloc(sizeof(struct node)); //memory allocation
  cin>>x;
  temp->data=x;
  temp->next=root;
  root=temp;
}
void deletenode()     // delete node from begning
{
    if(root==NULL)
    {
    cout<<"linked list is empty";
      return;
   }
  struct node *temp=root;
  root=temp->next;
  free(temp);
     
}

void print()  //for display the list
{
  struct node *temp;
  temp=root;
  while(temp!=NULL)
  {
  cout<<"-->"<<temp->data;
  temp=temp->next;
  }
}

void reverse()
{
  if(root==NULL)
  {
  return;
  }
    stack<struct node*> s; //create stack template for struct node type 
    struct node *temp;
    temp=root;
    while(temp!=NULL) // loop to push the refrence of node
    {
    s.push(temp);
    temp=temp->next;
    }
    temp=s.top();
    root=temp;
    s.pop();
    while(!s.empty())  // loop to poping the refrence of  node
    {
    temp->next=s.top();
    s.pop();
    temp=temp->next;
    }
    temp->next=NULL;
   
}
int main()
{
int n,i;
cout<<"Enter The Size of Linked List\n";
cin>>n;
 
  cout<<"Enter The Data in Linked List\n";
for(i=0;i<5;i++)
  {
  createnode();
}
cout<<"\nReverse Linked List is ";
  print();
  reverse();
  cout<<"\n\nOrigional Liked List is ";
  print();
 return 0;
}

Hacker Ranks Solution For Reverse Linked List

Node* Reverse(Node *head)
{
    if (head->next == NULL) 
    {
        return head;
    }

    Node* nextNode = head->next;
    head->next = NULL;
    Node* newHead = Reverse(nextNode);
    nextNode->next = head;
    return newHead;
}

Output:-

Reverse A Linked List Hacker Rank Solution In C++


Reverse A Linked List Hacker Rank Solution In C++

You May Like This:-

1. Hacker Rank Solution for 30 Days of Code

2. Hacker Rank solution for Attribute Parser

3. Java Program For Find The Gross Salary Of An Employee

4. C++ Program For School Management System ( SMS Project ) With Source Code

5. Hacker Rank Solution For Mini-Max Sum

6. Hacker Rank Solution For Birthday Cake Candles

7. C++ Program For Store Employee Information And Display Using Structure

8. C Program For Find A Grade Of Given Marks Using Switch Case

9. C Program For HEAP Sort In Ascending Order Also Show Complexity

10. C++ Program For Selection Sort Using Function

Extreme Recommended:- Like our Facebook Page or Join our Facebook Group and Google plus Community for up-to-date. If you have any Query or Question you can ask in the group, I will Try To Solve your Query and try to answers of your Questions withing 24 Hours, You can also Email me or comment below Please suggest to your Friends to join and like our page and don't forget to Subscribe. Enter your Email and click to subscribe.

subodh yadav

Ghanendra Yadav

Hello, I Am Ghanendra Yadav Owner of This Blog, I am professional Blogger and Programmer. I Love Programming, Logo Making, And Banner Designing. My Highest Qualification is MCA From NIT Warangal. You Can Find Me On Social Media Through Below Link And If You Have Any Query Related To Programming And Other Subject Comment Below or You Can Mail Me I Will Try To Answer Within 24 Hours Email:- yghanendra@student.nitw.ac.in

Find me on Social Media

Facebook | Twitter | Google+ | RSS Feed

No comments:

Post a Comment