-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1138.cpp
53 lines (46 loc) · 973 Bytes
/
1138.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
#include <iostream>
#include <list>
#include <vector>
using namespace std;
typedef struct __node{
int data;
struct __node* next;
}Node;
void insert(Node* head, int data, int n){
Node* cur = head;
for(int i=0; i<n; i++){
cur = cur->next;
}
Node* newnode= new Node;
newnode->data = data;
newnode->next = nullptr;
if(cur->next != nullptr)
newnode->next = cur->next;
cur->next = newnode;
}
void printNode(Node* head){
Node* cur = head->next;
while(cur!= nullptr){
cout << cur->data << ' ';
cur=cur->next;
}
cout << endl;
}
int n;
int main(void){
cin >> n;
Node* head = new Node;
head->data = -1;
head->next = nullptr;
vector<int> v(n);
for(int i=0; i<n; i++){
int num;
cin >> num;
v[i] = num;
}
for(auto iter=v.rbegin(); iter!=v.rend(); iter++,n--){
insert(head,n,*iter);
}
printNode(head);
return 0;
}