vault backup: 2022-06-15 22:53:24
This commit is contained in:
parent
b7b7bd23dc
commit
3dc4af3c98
|
@ -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]]
|
||||
|
@ -121,5 +144,13 @@ 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]]
|
||||
|
||||
|
||||
|
||||
|
|
@ -9,6 +9,8 @@
|
|||
#DS #linked_list
|
||||
##### Difficulty:
|
||||
#leetcode #coding_problem #difficulty-easy
|
||||
##### Lists:
|
||||
#CS_list_need_understanding #CS_list_need_practicing
|
||||
##### Related topics:
|
||||
```expander
|
||||
tag:#linked_list
|
||||
|
@ -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;
|
||||
}
|
||||
};
|
||||
```
|
|
@ -2,13 +2,18 @@
|
|||
|
||||
#### {{date}} {{time}}
|
||||
|
||||
---
|
||||
##### Algorithms:
|
||||
#algorithm
|
||||
##### Data structures:
|
||||
#DS
|
||||
##### Difficulty:
|
||||
#<CHANGE_ME> #coding_problem #difficulty-easy
|
||||
> ##### Algorithms:
|
||||
> #algorithm
|
||||
> ##### Data structures:
|
||||
> #DS
|
||||
> ##### Difficulty:
|
||||
> #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:
|
||||
```expander
|
||||
tag:#<INSERT_TAG_HERE>
|
||||
|
|
|
@ -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:#<CHANGE_ME> -tag:#template_remove_me
|
||||
|
|
Loading…
Reference in a new issue