-
Notifications
You must be signed in to change notification settings - Fork 30
/
linklistreverse.cpp
84 lines (81 loc) · 1.76 KB
/
linklistreverse.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(x) (x).begin(),(x).end()
#define vll vector<ll>
#define vl vector<ll>
#define pi pair<ll,ll>
#define vp vector<pi>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define rall(x) x.rbegin(), x.rend()
#define watch(x) cout << (#x) << ' is ' << (x) << endl
#define f(i,x,z) for(ll i=x;i<z;i++)
#define deb(x) cout<<'DEBUG------> '<<x<<endl
#define srt(v) sort(v.begin(), v.end())
#define clr(x) memset(x, 0, sizeof(x))
#define mll map<ll,ll>
#define mod 1000000007
class node{
public:
int data;
node* next;
node(int val){
data=val;
next=NULL;
}
};
node* reverse(node* &head){
node* prevptr=NULL;
node* currptr=head;
node* nextptr;
while(currptr!=NULL){
nextptr=currptr->next;
currptr->next=prevptr;
prevptr=currptr;
currptr=nextptr;
}
return prevptr; //New head
}
void insertAtTail(node* &head, int val)
{
node* n = new node(val);
if(head==NULL)
{
head=n;
return;
}
node* temp=head;
while(temp->next!=NULL){
temp=temp->next;
}
temp->next=n;
}
void display(node* head)
{
node* temp=head;
while(temp!=NULL){
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<endl;
}
int main()
{
// freopen('input.txt', 'r', stdin);
// freopen('output.txt', 'w', stdout);
ios::sync_with_stdio(0);
cin.tie(0);
node* head=NULL;
insertAtTail(head, 1);
insertAtTail(head, 2);
insertAtTail(head, 3);
insertAtTail(head, 4);
display(head);
head=reverse(head);
display(head);
return 0;
}