vault backup: 2022-06-15 22:53:24

This commit is contained in:
juan 2022-06-15 22:53:24 +08:00
parent b7b7bd23dc
commit 3dc4af3c98
4 changed files with 79 additions and 17 deletions

View file

@ -12,6 +12,28 @@
This is where I store notes about CS 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 ## Websites
#### [leetcode.com](https://leetcode.com) #### [leetcode.com](https://leetcode.com)
@ -32,6 +54,7 @@ tag:#leetcode
- [[Leetcode Ransom-Note]] - [[Leetcode Ransom-Note]]
- [[Leetcode Remove-Linked-List-Elements]] - [[Leetcode Remove-Linked-List-Elements]]
- [[Leetcode Reshape-The-Matrix]] - [[Leetcode Reshape-The-Matrix]]
- [[Leetcode Reverse-Linked-List]]
- [[Leetcode Search-a-2D-Matrix]] - [[Leetcode Search-a-2D-Matrix]]
- [[Leetcode Two-Sum]] - [[Leetcode Two-Sum]]
- [[Leetcode Valid-Anagram]] - [[Leetcode Valid-Anagram]]
@ -121,5 +144,13 @@ tag:#algorithm tag:#CS_analysis -tag:#template_remove_me
- [[$filename]] - [[$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]]

View file

@ -9,6 +9,8 @@
#DS #linked_list #DS #linked_list
##### Difficulty: ##### Difficulty:
#leetcode #coding_problem #difficulty-easy #leetcode #coding_problem #difficulty-easy
##### Lists:
#CS_list_need_understanding #CS_list_need_practicing
##### Related topics: ##### Related topics:
```expander ```expander
tag:#linked_list tag:#linked_list
@ -53,7 +55,29 @@ Given the `head` of a singly linked list, reverse the list, and return _the reve
### Thoughts ### Thoughts
> [!summary] I thought a slow O(n ^ 2) hybrid solution, while there are better algorithms, using in-place insert, or recursion.
> This is a #template_remove_me
The in place insert is easier to understand, and simple to implement, using a very clever trick.
### Solution ### 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;
}
};
```

View file

@ -2,13 +2,18 @@
#### {{date}} {{time}} #### {{date}} {{time}}
--- > ##### Algorithms:
##### Algorithms: > #algorithm
#algorithm > ##### Data structures:
##### Data structures: > #DS
#DS > ##### Difficulty:
##### Difficulty: > #coding_problem #difficulty-easy
#<CHANGE_ME> #coding_problem #difficulty-easy > ##### Additional tags:
> #<platforms-like-leetcode> #<list-to-be-added>
> ##### Revisions:
> Initial encounter: {{date}}
> 1st revision: <use-template-command-to-insert-date>
##### Related topics: ##### Related topics:
```expander ```expander
tag:#<INSERT_TAG_HERE> tag:#<INSERT_TAG_HERE>

View file

@ -2,13 +2,15 @@
#### {{date}} {{time}} #### {{date}} {{time}}
___ > ##### Algorithms:
##### Algorithms: > #algorithm
#algorithm > ##### Data structures:
##### Data structures: > #DS
#DS > ##### Difficulty:
##### Difficulty: > #CS_analysis #difficulty-
#CS_analysis #difficulty- > ##### Additional tags:
>
##### Related problems: ##### Related problems:
```expander ```expander
tag:#coding_problem tag:#<CHANGE_ME> -tag:#template_remove_me tag:#coding_problem tag:#<CHANGE_ME> -tag:#template_remove_me