add: 2021-12-22
This commit is contained in:
parent
0540982c10
commit
dcdda63a84
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,6 @@
|
||||||
|
CMAKE_MINIMUM_REQUIRED(VERSION 3.21)
|
||||||
|
PROJECT(2112)
|
||||||
|
|
||||||
|
SET(CMAKE_CXX_STANDARD 23)
|
||||||
|
|
||||||
|
ADD_EXECUTABLE(2112 211222.cpp)
|
||||||
|
|
@ -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)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue