forked from shriyajalana/programming1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList2.cpp
More file actions
55 lines (53 loc) · 904 Bytes
/
Copy pathLinkedList2.cpp
File metadata and controls
55 lines (53 loc) · 904 Bytes
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
//reverse a linked list using recurssion method
#include<iostream>
using namespace std;
struct Node{
int data;
Node* next;
};
Node* head;
void Insert(int n){
Node* temp=new Node;
temp->data=n;
temp->next=NULL;
if(head==NULL){
head=temp;
return;
}
else{
Node* temp1=head;
while(temp1->next!=NULL){
temp1=temp1->next;
}
temp1->next=temp;
}
}
void Print(){
Node* temp=head;
while(temp!=NULL){
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<endl;
}
void ReversePrint(Node* p){
if(p->next==NULL){
head=p;
return;
}
ReversePrint(p->next);
Node* q=p->next;
q->next=p;
p->next=NULL;
}
int main(){
head=NULL;
Insert(4);
Insert(7);
Insert(2);
Insert(8);
Insert(9);
ReversePrint(head);
Print();
return 0;
}