diff --git a/.vscode/settings.json b/.vscode/settings.json index ee8c403..16c52fb 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -68,6 +68,26 @@ "cinttypes": "cpp", "typeindex": "cpp", "typeinfo": "cpp", - "variant": "cpp" + "variant": "cpp", + "__bit_reference": "cpp", + "__config": "cpp", + "__debug": "cpp", + "__errc": "cpp", + "__functional_base": "cpp", + "__hash_table": "cpp", + "__locale": "cpp", + "__mutex_base": "cpp", + "__node_handle": "cpp", + "__nullptr": "cpp", + "__split_buffer": "cpp", + "__std_stream": "cpp", + "__string": "cpp", + "__threading_support": "cpp", + "__tree": "cpp", + "__tuple": "cpp", + "ios": "cpp", + "locale": "cpp", + "queue": "cpp", + "stack": "cpp" } } \ No newline at end of file diff --git a/0024.h b/0024.h new file mode 100644 index 0000000..3206e67 --- /dev/null +++ b/0024.h @@ -0,0 +1,28 @@ +#include "common.h" + +namespace s0024 { + // Definition for singly-linked list. + 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: + ListNode* swapPairs(ListNode* head) { + ListNode* dummyHead = new ListNode(0, head); + ListNode* cur = dummyHead; + while (cur->next != nullptr && cur->next->next != nullptr) { + ListNode* tmp = cur->next; + ListNode* tmp1 = cur->next->next->next; + cur->next = cur->next->next; + cur->next->next = tmp; + cur->next->next->next = tmp1; + cur = cur->next->next; + } + return dummyHead->next; + } + }; +} \ No newline at end of file diff --git a/test.cc b/test.cc index 4f0cc50..4560439 100644 --- a/test.cc +++ b/test.cc @@ -1,10 +1,33 @@ #include "0001.h" +#include "0024.h" #include - -TEST(S001, Normal) { +TEST(S0001, Normal) { s0001::Solution s; - std::vector v = {1,2,3}; - // std::vector r = {0,1}; - EXPECT_EQ(std::vector({0,1}), s.twoSum(v, 3)); -} \ No newline at end of file + std::vector v1 = {1,2,3}; + std::vector v2 = {1,1,1}; + EXPECT_EQ(std::vector({0,1}), s.twoSum(v1, 3)); + EXPECT_EQ(std::vector({0,1}), s.twoSum(v2, 2)); +} + +s0024::ListNode* createList(vector v) { + s0024::ListNode* head = new s0024::ListNode(v[0]); + s0024::ListNode* tmp = head; + for (int i=1; inext = node; + tmp = tmp->next; + } + return head; +} + +TEST(S0024, Normal) { + s0024::Solution s; + s0024::ListNode* t1 = createList(vector{1,2,3,4}); + s0024::ListNode* r = s.swapPairs(t1); + vector exp({2,1,4,3}); + for (int i=0; ival); + r = r->next; + } +}