QUESTION: Reverse the given linked list:
WRITE CODE IN LINK LIST AND ADD COMMENTS TO THE CODE PLEASED MANDATORY
QUESTION: Reverse the given linked list:
For example:
Input: 100 > 200 > 300 > 400 > 500 > 600
Reversed: 600 > 500 > 400 > 300 > 200 > 100
I HAVE RECIEVED A CODE BUT ITS NOT COMPILING AND SHOWS ERROR PLEASE CHECK IT OUT AND SEND ME WITH COMMENTS.
MAKE SURE IT COMPILES AND MAKE SURE YOU ADD THE COMMENTS
CODE:
#include<iostream>
using namespace std;
struct Node {
int data;
struct Node* next;
Node(int data)
{
this->data=data;
next=NULL;
}
};
struct LinkedList {
Node* head;
LinkedList( ) {head=NULL;}
void reverse( )
{
Node* current=head;
Node*prev=NULL, *next=NULL;
while (current!=NULL) {
next=current->next;
current->next=prev;
prev=current;
current=next;
}
head=prev;
}
void print( )
{
struct Node* temp=head;
while (temp!=NULL) {
cout<<temp->data<<" ";
temp=temp->next;
}
}
void push(int data)
{
Node*temp= new Node(data);
temp->next=head;
head=temp;
}
};
int main( )
{
LinkedList 11;
11.push(600);
11.push(500);
11.push(400);
11.push(300);
11.push(200);
11.push(100);
cout<<"Given linked list\n";
11.print( );
11.reverse( );
cout<<"\nReversed linked list \n";
11.print( );
return 0;
}
MUST ADD THE COMMENTS
Trending now
This is a popular solution!
Step by step
Solved in 2 steps