aboutsummaryrefslogtreecommitdiffstats
path: root/1721/main.cpp
diff options
context:
space:
mode:
authorterminaldweller <thabogre@gmail.com>2022-04-04 17:10:06 +0000
committerterminaldweller <thabogre@gmail.com>2022-04-04 17:10:06 +0000
commitcb4f4807e4cd0baf712efef100b07289c0c7ed47 (patch)
treebc7c3408b5f414e3f795f44fd82e9f777649b41f /1721/main.cpp
parentupdate (diff)
downloadleetcode-cb4f4807e4cd0baf712efef100b07289c0c7ed47.tar.gz
leetcode-cb4f4807e4cd0baf712efef100b07289c0c7ed47.zip
update
Diffstat (limited to '')
-rw-r--r--1721/main.cpp57
1 files changed, 57 insertions, 0 deletions
diff --git a/1721/main.cpp b/1721/main.cpp
new file mode 100644
index 0000000..e707f35
--- /dev/null
+++ b/1721/main.cpp
@@ -0,0 +1,57 @@
+#include "header.hpp"
+
+struct ListNode {
+ int val;
+ ListNode *next;
+ ListNode() : val(0), next(nullptr) {}
+ ListNode(int x) : val(x), next(nullptr) {}
+ ListNode(int x, ListNode *next) : val(x), next(next) {}
+};
+
+class Solution {
+public:
+ static ListNode *swapNodes(ListNode *head, int k) {
+ auto iter = head;
+ int counter = 1;
+ std::vector<ListNode *> dummy;
+ while (iter->next != nullptr) {
+ dummy.push_back(iter);
+ iter = iter->next;
+ counter++;
+ }
+ dummy.push_back(iter);
+
+ std::cout << "counter: " << counter << "\n";
+
+ std::swap(static_cast<ListNode *>(dummy[k - 1])->val,
+ static_cast<ListNode *>(dummy[counter - k])->val);
+
+ return head;
+ }
+};
+
+int main(int argc, char **argv) {
+ ListNode *l1 = new ListNode;
+ ListNode *l2 = new ListNode;
+ ListNode *l3 = new ListNode;
+ ListNode *l4 = new ListNode;
+ ListNode *l5 = new ListNode;
+ l1->val = 1;
+ l2->val = 2;
+ l3->val = 3;
+ l4->val = 4;
+ l5->val = 5;
+ l1->next = l2;
+ l2->next = l3;
+ l3->next = l4;
+ l4->next = l5;
+ l5->next = nullptr;
+
+ auto head = Solution::swapNodes(l1, 2);
+
+ while (head->next != nullptr) {
+ std::cout << head->val << ",";
+ head = head->next;
+ }
+ std::cout << "\n";
+}