-
Notifications
You must be signed in to change notification settings - Fork 0
/
test01.cpp
92 lines (82 loc) · 1.68 KB
/
test01.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
85
86
87
88
89
90
91
92
#include<iostream>
#include<vector>
#include<stdlib.h>
#include<algorithm>
using namespace std;
struct ListNode {
int val;
ListNode* next;
};
void addListNode(ListNode* head, int n)
{
ListNode* p = head;
ListNode* newNode = new ListNode;
while (p->next)
{
p = p->next;
}
newNode->val = n;
newNode->next = nullptr;
p->next = newNode;
}
void printNode(ListNode* head)
{
ListNode* p = head->next;
while (p)
{
cout << p->val << ' ';
p = p->next;
}
cout << endl;
}
void reversevector(vector<int>& A) {
int i = 0;
int j = A.size() - 1;
while (i < j) {
swap(A[i], A[j]);
i++;
j--;
}
}
//·½·¨¶þ£ºµ¥Á´±í
void reverselist(ListNode* head) {
ListNode* first = head->next;
ListNode* prev = nullptr;
ListNode* curr = first;
ListNode* next = curr->next;
while (curr) {
ListNode* next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
first = prev;
head->next = first;
}
int main()
{
cout << "list:" << endl;
ListNode list{};
for (int i = 1; i < 10; i++)
{
addListNode(&list, i * 2 + rand() % 100);
//addListNode(&list, i * 2 + rand()%100);
}
printNode(&list);
reverselist(&list);
printNode(&list);
cout << "----------------------" << endl;
cout << "vector:" << endl;
vector<int> vec{ 1,2,3,4,5,6,7,8,9 };
for (auto n : vec)
cout << n << ' ';
cout << endl;
reversevector(vec);
for (auto n : vec)
cout << n << ' ';
cout << endl;
reverse(vec.begin(), vec.end());
for (auto n : vec)
cout << n << ' ';
cout << endl;
}