From 3dc4af3c98cb01b39728db96d1251d22a1d2e0ad Mon Sep 17 00:00:00 2001 From: juan Date: Wed, 15 Jun 2022 22:53:24 +0800 Subject: [PATCH] vault backup: 2022-06-15 22:53:24 --- CS notes/CS-index.md | 31 +++++++++++++++++++ .../pages/Leetcode Reverse-Linked-List.md | 30 ++++++++++++++++-- _templates/OJ Problem Notes.md | 19 +++++++----- _templates/cpp_stuff_explainer.md | 16 +++++----- 4 files changed, 79 insertions(+), 17 deletions(-) diff --git a/CS notes/CS-index.md b/CS notes/CS-index.md index 890e176..2da143d 100644 --- a/CS notes/CS-index.md +++ b/CS notes/CS-index.md @@ -12,6 +12,28 @@ This is where I store notes about CS ___ + +## Lists +- Needs more understandings #CS_list_need_understanding +```expander +tag:#CS_list_need_understanding +- [ ] [[$filename]] +``` + +- [ ] [[Leetcode Reverse-Linked-List]] + + + +- Needs practicing #CS_list_need_practicing +```expander +tag:#CS_list_need_practicing +- [ ] [[$filename]] +``` + +- [ ] [[Leetcode Reverse-Linked-List]] + + + ## Websites #### [leetcode.com](https://leetcode.com) @@ -32,6 +54,7 @@ tag:#leetcode - [[Leetcode Ransom-Note]] - [[Leetcode Remove-Linked-List-Elements]] - [[Leetcode Reshape-The-Matrix]] +- [[Leetcode Reverse-Linked-List]] - [[Leetcode Search-a-2D-Matrix]] - [[Leetcode Two-Sum]] - [[Leetcode Valid-Anagram]] @@ -120,6 +143,14 @@ tag:#algorithm tag:#coding_problem -tag:#template_remove_me tag:#algorithm tag:#CS_analysis -tag:#template_remove_me - [[$filename]] ``` + +- [[Binary Search Algorithm]] +- [[Binary Search Algorithm.sync-conflict-20220615-121016-T44CT3O]] +- [[cpp_std_sort]] +- [[Floyd's Cycle Finding Algorithm]] +- [[Kadane's Algorithm]] +- [[Two pointers approach]] + \ No newline at end of file diff --git a/CS notes/pages/Leetcode Reverse-Linked-List.md b/CS notes/pages/Leetcode Reverse-Linked-List.md index a5669d4..e89e056 100644 --- a/CS notes/pages/Leetcode Reverse-Linked-List.md +++ b/CS notes/pages/Leetcode Reverse-Linked-List.md @@ -9,11 +9,13 @@ #DS #linked_list ##### Difficulty: #leetcode #coding_problem #difficulty-easy +##### Lists: +#CS_list_need_understanding #CS_list_need_practicing ##### Related topics: ```expander tag:#linked_list ``` - + ##### Links: @@ -53,7 +55,29 @@ Given the `head` of a singly linked list, reverse the list, and return _the reve ### Thoughts -> [!summary] -> This is a #template_remove_me +I thought a slow O(n ^ 2) hybrid solution, while there are better algorithms, using in-place insert, or recursion. + +The in place insert is easier to understand, and simple to implement, using a very clever trick. ### Solution +I've referred to this guy: https://leetcode.com/problems/reverse-linked-list/discuss/58130/C%2B%2B-Iterative-and-Recursive + +```cpp +class Solution { +public: + ListNode *reverseList(ListNode *head) { + ListNode *pre = new ListNode(0), *cur = head; + // pre is before head, and insert any element after pre. + pre->next = head; + while (cur && cur->next) { + // Move cur->next after pre. + ListNode *temp = pre->next; + pre->next = cur->next; + cur->next = cur->next->next; + + pre->next->next = temp; + } + return pre->next; + } +}; +``` \ No newline at end of file diff --git a/_templates/OJ Problem Notes.md b/_templates/OJ Problem Notes.md index a2383af..ebad482 100644 --- a/_templates/OJ Problem Notes.md +++ b/_templates/OJ Problem Notes.md @@ -2,13 +2,18 @@ #### {{date}} {{time}} ---- -##### Algorithms: -#algorithm -##### Data structures: -#DS -##### Difficulty: -# #coding_problem #difficulty-easy +> ##### Algorithms: +> #algorithm +> ##### Data structures: +> #DS +> ##### Difficulty: +> #coding_problem #difficulty-easy +> ##### Additional tags: +> # # +> ##### Revisions: +> Initial encounter: {{date}} +> 1st revision: + ##### Related topics: ```expander tag:# diff --git a/_templates/cpp_stuff_explainer.md b/_templates/cpp_stuff_explainer.md index cb5c84e..2814673 100644 --- a/_templates/cpp_stuff_explainer.md +++ b/_templates/cpp_stuff_explainer.md @@ -2,13 +2,15 @@ #### {{date}} {{time}} -___ -##### Algorithms: -#algorithm -##### Data structures: -#DS -##### Difficulty: -#CS_analysis #difficulty- +> ##### Algorithms: +> #algorithm +> ##### Data structures: +> #DS +> ##### Difficulty: +> #CS_analysis #difficulty- +> ##### Additional tags: +> + ##### Related problems: ```expander tag:#coding_problem tag:# -tag:#template_remove_me