Reverse A LL
You have been given a singly linked list of integers. Write a function to print the list in a reverse order.
To explain it further, you need to start printing the data from the tail and move towards the head of the list, printing the head data at the end.
Note :
Input format :
Remember/Constraints :
Output format :
Constraints :
Sample Input 1 :
Sample Output 1 :
Sample Input 2 :
Sample Output 2 :
50 40 30 20 10
/****************************************************************
Following is the class structure of the Node class:
class Node
{
public:
int data;
Node *next;
Node(int data)
{
this->data = data;
this->next = NULL;
}
};
*****************************************************************/
void display(Node* head){
Node* temp=head;
while(temp!=NULL){
cout<<temp->data<<" ";
temp=temp->next;
}
}
Node* printReverse(Node *head)
{
//Write your code here
Node* prv = NULL;
Node* cur = head;
Node* nxt;
while(cur!=NULL){
nxt=cur->next;
cur->next=prv;
prv=cur;
cur=nxt;
}
display(prv);
return prv;
}
Comments
Post a Comment