Reverse-linked list

#include
#include
#include
using namespace std;
class node{
public:
int data;
node *next;

node(int d){
	data = d;
	next = NULL;
}

};

void insertathead(node*&head,int d){
if(head==NULL){
head = new node(d);
return;
}

node*n = new node(d);
n->next = head;
head = n;

}
vector v;

void reverselist(node*&head){
while(head!=NULL){
v.push_back(head->data);
head = head->next;
}
reverse(v.begin(),v.end());
for(auto x:v){
cout<<x<<" ";
}
cout<<endl;
}

int main() {
int n;
cin>>n;
node*head=NULL;
for(int i=0;i<n;i++){
int d;
cin>>d;
insertathead(head,d);
}
reverselist(head);
return 0;
}

hi @abhinavssr2003_eab0717682969e5c, send the code on ide.codingblocks.com

hi @abhinavssr2003_eab0717682969e5c as u r inserting at head no need to reverse vector v

didn’t get you…will you please update my code once

IT PASSED…but i don’t understand why the list reversed without using the reverse function…like i am adding the data from the begin

Coz u r adding at head its always getting added at head

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.