forked from v100901/hackoctoberfest2020__
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReverse Linked List
61 lines (61 loc) · 921 Bytes
/
Reverse Linked List
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
```#include<iostream>
using namespace std;
class node{
public:
int data;
node*next;
node(int data){
this->data=data;
this->next=NULL;
}
};
node* takeinput(){
int data=0;
node*head=NULL;
node*tail=NULL;
cout<<"enter the data of your linked list, enter -1 to stop:"<<endl;
cin>>data;
while(data!=-1){
node*n=new node(data);
if(head==NULL){
head=n;
tail=n;
}
else{
tail->next=n;
tail=n;
}
cin>>data;
}
return head;
}
void print(node*head)
{
while(head!=NULL){
cout<<head->data<<" ";
head=head->next;
}
cout<<endl;
return;
}
node*reversell(node*head){
node*head1=head;
node*tail=head->next;
head->next=NULL;
head=tail;
while(head->next!=NULL){
tail=head->next;
head->next=head1;
head1=head;
head=tail;
}
head->next=head1;
return head;
}
int main()
{
node*n=takeinput();
node*n1=reversell(n);
print(n1);
return 0;
}```