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.
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:-
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
0 Comments: