From e0010f865d2e94c7afbef755d273a97579c46072 Mon Sep 17 00:00:00 2001 From: Lam Haoyin Date: Thu, 24 Feb 2022 17:05:43 +0800 Subject: [PATCH] add: 220224 [cpp] --- cpp/2202/220224.cpp | 37 +++++++++++++++++++++++++++++++++++++ cpp/2202/CMakeLists.txt | 2 +- 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 cpp/2202/220224.cpp diff --git a/cpp/2202/220224.cpp b/cpp/2202/220224.cpp new file mode 100644 index 0000000..209d4b0 --- /dev/null +++ b/cpp/2202/220224.cpp @@ -0,0 +1,37 @@ +#include +#include + +/** + * Definition for singly-linked list. + */ +struct ListNode { + int val; + ListNode* next; + + ListNode(int x = 0, ListNode* next = nullptr) : val(x), next(next) {} +}; + +/** + * 148. Sort List + * Given the head of a linked list, return the list after sorting it in ascending order. + */ +class Solution { +public: + static ListNode* sortList(ListNode* head) { + if (!head) + return nullptr; + std::vector arr; + for (ListNode* ptr = head; ptr; ptr = ptr->next) + arr.push_back(ptr); + std::sort(arr.begin(), arr.end(), [](ListNode* x, ListNode* y){ return x->val < y->val; }); + int n = arr.size(); + for (int i = 0; i < n - 1; ++i) + arr[i]->next = arr[1 + i]; + arr.back()->next = nullptr; + return arr[0]; + } +}; + +int main() { + return 0; +} diff --git a/cpp/2202/CMakeLists.txt b/cpp/2202/CMakeLists.txt index eeede82..7f7ca6f 100644 --- a/cpp/2202/CMakeLists.txt +++ b/cpp/2202/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2202) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2202 220224-CN.cpp) +ADD_EXECUTABLE(2202 220224.cpp)