-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreverseLinkedList.cpp
57 lines (55 loc) · 1.08 KB
/
reverseLinkedList.cpp
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
#include <bits/stdc++.h>
using namespace std ;
int sie ; // global variable
class node {
public:
int data;
node *next ;
};
node *first , *last ;
void create(int *p , int size ) {
node *ptr = new node ;
first = ptr ;
ptr->data = p[0] ;
ptr->next = nullptr ;
last = ptr ;
for(int i = 1 ; i<size ; i++){
node *ptr = new node ;
ptr->data = *(p+i);
last->next = ptr ;
last = ptr ;
last->next = nullptr ;
}
sie = size ;}
void display(node *ptr){
while (ptr!=nullptr)
{
cout<<ptr->data<<" ";
ptr=ptr->next ;
/* code */
}
cout<<endl;
}
void reverseLinkedList_method_1(){
int ctr = 0 ;
node *ptr = first ;
vector <int> vec ;
while(ptr!=nullptr){
vec.push_back(ptr->data);
ptr=ptr->next ;
ctr++;
}
ptr = first ;
while(ptr!=nullptr){
ptr->data = vec.at(ctr-1) ; ctr-- ;
ptr = ptr->next ;
}
}
int main(){
int a[5] = {40 , 400 , 4 , 14 , 44 };
create(a,5);
display(first);
reverseLinkedList_method_1() ;
display(first); // kalashnikov
return 0 ;
}