add: 2021-12-22

This commit is contained in:
Lam Haoyin 2021-12-25 11:57:32 +08:00
parent 0540982c10
commit dcdda63a84
No known key found for this signature in database
GPG Key ID: 8C089CB1A2B7544F
3 changed files with 59 additions and 4 deletions

47
2112/211222.cpp Normal file
View File

@ -0,0 +1,47 @@
#include <vector>
#include <cstdio>
// 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;
}

6
2112/CMakeLists.txt Normal file
View File

@ -0,0 +1,6 @@
CMAKE_MINIMUM_REQUIRED(VERSION 3.21)
PROJECT(2112)
SET(CMAKE_CXX_STANDARD 23)
ADD_EXECUTABLE(2112 211222.cpp)

View File

@ -1,6 +1,8 @@
cmake_minimum_required(VERSION 3.21) CMAKE_MINIMUM_REQUIRED(VERSION 3.21)
project(LeetCodeDaily) 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)