-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem_013.cpp
More file actions
62 lines (51 loc) · 1.08 KB
/
problem_013.cpp
File metadata and controls
62 lines (51 loc) · 1.08 KB
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
62
//Write a program to reverse a linkedlist
#include<iostream>
#include<vector>
using namespace std;
struct Node{
int data;
struct Node* next;
};
void CreateList(struct Node* *root,int value){
struct Node* ptr=*root;
struct Node* temp= new Node();
temp->data=value;
temp->next=NULL;
if(*root == NULL){
*root=temp;
}else{
while(ptr->next !=NULL){
ptr=ptr->next;
}
ptr->next=temp;
}
}
void reverse(struct Node* root){
struct Node* ptr= root;
vector<int> v;
while(ptr != NULL){
v.push_back(ptr->data);
ptr=ptr->next;
}
for (auto it = v.rbegin(); it != v.rend(); ++it) {
cout << *it << "-> ";
}
cout<<endl;
}
void display(struct Node* root){
struct Node* ptr=root;
while(ptr!=NULL){
cout<<ptr->data<<"->";
ptr=ptr->next;
}
cout<<"Null"<<endl;
}
int main(){
struct Node* root=NULL;
CreateList(&root,12);
CreateList(&root,15);
CreateList(&root,17);
CreateList(&root,22);
reverse(root);
return 0;
}