-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlinkedlist.cpp
More file actions
76 lines (68 loc) · 1.35 KB
/
linkedlist.cpp
File metadata and controls
76 lines (68 loc) · 1.35 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <sstream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <queue>
#include <stack>
#include <map>
#include <cstdio>
#include <cstring>
#include <cassert>
using namespace std;
#define FOR(i,a,n) for(int i=int(a);i<int(n);i++)
#define REP(i,n) FOR(i,0,n)
#define FORE(it, c) for(typeof(c.begin()) it = c.begin(); it != c.end(); it++)
#define ALL(c) c.begin(), c.end()
#define CLEAR(c,v) memset(c,v,sizeof(c))
typedef long long int lli;
typedef pair<int,int> ii;
struct node{
int val;
node *next;
};
node *head = NULL;
void append(int x){
if(head == NULL){
head = new node;
head->val = x;
head->next = NULL;
}
else{
node *p = head;
while(p->next != NULL)
p = p->next;
p->next = new node;
p->next->val = x;
p->next->next = NULL;
}
}
void print(){
node *p = head;
while(p != NULL){
cout << p->val << ' ';
p = p->next;
}
cout << endl;
}
void rev(){
if(head -> next == NULL) return;
node *a = head, *b = head -> next;
a -> next = NULL;
while(b != NULL){
node *c = b -> next;
b -> next = a;
a = b;
b = c;
}
head = a;
}
int main() {
int n = 10;
REP(i,n) append(i);
print();
rev();
print();
}