-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1721.cpp
More file actions
61 lines (49 loc) · 1.1 KB
/
1721.cpp
File metadata and controls
61 lines (49 loc) · 1.1 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
#include<iostream>
#include<vector>
#include<algorithm>
class ListNode {
public:
ListNode* next;
int val;
};
ListNode* swapNodes(ListNode* head, int k) {
std::vector<int> vec;
ListNode* currNode = head;
while (currNode != nullptr) {
vec.push_back(currNode->val);
currNode = currNode->next;
}
std::swap(vec[k - 1], vec[vec.size() - k]);
currNode = head;
int i = 0;
while (currNode != nullptr) {
std::cout << "vec: " << vec[i] << "\n";
currNode->val = vec[i];
currNode = currNode->next;
++i;
}
currNode = head;
return currNode;
}
int main() {
int k = 2;
ListNode* n1 = new ListNode();
ListNode* n2 = new ListNode();
ListNode* n3 = new ListNode();
ListNode* n4 = new ListNode();
ListNode* n5 = new ListNode();
ListNode* head = n1;
n1->next = n2;
n2->next = n3;
n3->next = n4;
n4->next = n5;
n5->next = nullptr;
head->val = 34;
n1->val = 35;
n2->val = 36;
n3->val = 37;
n4->val = 38;
n5->val = 39;
swapNodes(head, k);
std::cin.get();
}