From dcdda63a84bf8c37deed2bc3cd1183bc35923606 Mon Sep 17 00:00:00 2001 From: Lam Haoyin Date: Sat, 25 Dec 2021 11:57:32 +0800 Subject: [PATCH] add: 2021-12-22 --- 2112/211222.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++++ 2112/CMakeLists.txt | 6 ++++++ CMakeLists.txt | 10 ++++++---- 3 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 2112/211222.cpp create mode 100644 2112/CMakeLists.txt diff --git a/2112/211222.cpp b/2112/211222.cpp new file mode 100644 index 0000000..9d6ceee --- /dev/null +++ b/2112/211222.cpp @@ -0,0 +1,47 @@ +#include +#include + +// 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) {} + + ~ListNode() { delete this->next; } +}; + +class Solution { +public: + static void reorderList(ListNode* head) { + ListNode* nodes[50005]; + int n = 0; + do { + nodes[n++] = head; + } while ((head = head->next) != nullptr); + for (int i = 0, j = n - 1; i <= j; ++i, --j) { + auto* tmp = nodes[i]->next; + nodes[i]->next = nodes[j]; + nodes[j]->next = nodes[i] == nodes[i]->next || tmp == nodes[j] ? nullptr : tmp; + } + } +}; + +int main() { + auto* H = new ListNode(1); + auto* ptr = H; + for (int i = 2; i <= 17; ++i) { + ptr->next = new ListNode(i); + ptr = ptr->next; + } + Solution::reorderList(H); + ptr = H; + do { + std::printf("%d ", ptr->val); + } while ((ptr = ptr->next) != nullptr); + delete H; +} \ No newline at end of file diff --git a/2112/CMakeLists.txt b/2112/CMakeLists.txt new file mode 100644 index 0000000..fead261 --- /dev/null +++ b/2112/CMakeLists.txt @@ -0,0 +1,6 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 3.21) +PROJECT(2112) + +SET(CMAKE_CXX_STANDARD 23) + +ADD_EXECUTABLE(2112 211222.cpp) \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 4a5bffa..4bced43 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,8 @@ -cmake_minimum_required(VERSION 3.21) -project(LeetCodeDaily) +CMAKE_MINIMUM_REQUIRED(VERSION 3.21) +PROJECT(LeetCodeDaily) -set(CMAKE_CXX_STANDARD 23) +SET(CMAKE_CXX_STANDARD 23) -add_executable(LeetCodeDaily main.cpp) +ADD_EXECUTABLE(LeetCodeDaily main.cpp) + +ADD_SUBDIRECTORY(2112)