From 74ebcda764b90d69968250c302dce34f60396d4d Mon Sep 17 00:00:00 2001 From: juan Date: Wed, 15 Jun 2022 22:12:54 +0800 Subject: [PATCH] vault backup: 2022-06-15 22:12:54 --- CS notes/CS-index.md | 11 +- .../Leetcode Remove-Linked-List-Elements.md | 130 ++++++++++++++++-- .../pages/Leetcode Reverse-Linked-List.md | 35 +++++ 3 files changed, 160 insertions(+), 16 deletions(-) create mode 100644 CS notes/pages/Leetcode Reverse-Linked-List.md diff --git a/CS notes/CS-index.md b/CS notes/CS-index.md index aac938f..890e176 100644 --- a/CS notes/CS-index.md +++ b/CS notes/CS-index.md @@ -30,6 +30,7 @@ tag:#leetcode - [[Leetcode Merge-Two-Sorted-Lists]] - [[Leetcode Pascal's-Triangle]] - [[Leetcode Ransom-Note]] +- [[Leetcode Remove-Linked-List-Elements]] - [[Leetcode Reshape-The-Matrix]] - [[Leetcode Search-a-2D-Matrix]] - [[Leetcode Two-Sum]] @@ -66,6 +67,7 @@ tag:#DS tag:#coding_problem -tag:#template_remove_me - [[Leetcode Merge-Two-Sorted-Lists]] - [[Leetcode Pascal's-Triangle]] - [[Leetcode Ransom-Note]] +- [[Leetcode Remove-Linked-List-Elements]] - [[Leetcode Reshape-The-Matrix]] - [[Leetcode Search-a-2D-Matrix]] - [[Leetcode Two-Sum]] @@ -80,6 +82,7 @@ tag:#DS tag:#CS_analysis -tag:#template_remove_me - [[$filename]] ``` +- [[Binary Search Algorithm.sync-conflict-20220615-121016-T44CT3O]] - [[cpp_Range_based_for_loop]] - [[cpp_std_multiset]] - [[cpp_std_unordered_map]] @@ -117,12 +120,6 @@ tag:#algorithm tag:#coding_problem -tag:#template_remove_me tag:#algorithm tag:#CS_analysis -tag:#template_remove_me - [[$filename]] ``` - -- [[Binary Search Algorithm]] -- [[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 Remove-Linked-List-Elements.md b/CS notes/pages/Leetcode Remove-Linked-List-Elements.md index 5353079..c0e2290 100644 --- a/CS notes/pages/Leetcode Remove-Linked-List-Elements.md +++ b/CS notes/pages/Leetcode Remove-Linked-List-Elements.md @@ -3,33 +3,145 @@ #### 2022-06-15 21:50 --- -##### Algorithms: -#algorithm ##### Data structures: -#DS +#DS #linked_list ##### Difficulty: -# #coding_problem #difficulty-easy +#leetcode #coding_problem #difficulty-easy ##### Related topics: ```expander -tag:# +tag:#linked_list ``` +- [[Floyd's Cycle Finding Algorithm]] +- [[Leetcode Linked-List-Cycle]] +- [[Leetcode Merge-Two-Sorted-Lists]] +- [[Two pointers approach]] ##### Links: -- [Link to problem]() +- [Link to problem](https://leetcode.com/problems/remove-linked-list-elements/) +- [Additional Solutions](https://leetcode.com/problems/remove-linked-list-elements/discuss/722528/C++-2-solutions:-With-single-pointer-+-With-double-pointers-(Easy-to-understand)/1390186) ___ ### Problem +Given the `head` of a linked list and an integer `val`, remove all the nodes of the linked list that has `Node.val == val`, and return _the new head_. #### Examples +**Example 1:** + +![](https://assets.leetcode.com/uploads/2021/03/06/removelinked-list.jpg) + ```markdown +**Input:** head = [1,2,6,3,4,5,6], val = 6 +**Output:** [1,2,3,4,5] ``` +**Example 2:** + +```markdown +**Input:** head = [], val = 1 +**Output:** [] +``` + +**Example 3:** + +```markdown +**Input:** head = [7,7,7,7], val = 7 +**Output:** [] +``` #### Constraints +- The number of nodes in the list is in the range `[0, 104]`. +- `1 <= Node.val <= 50` +- `0 <= val <= 50` ### Thoughts -> [!summary] -> This is a #template_remove_me - +Simple linked list operations, but remember to check for special cases: +- The pointer is null ### Solution + +Two pointers, O(n) +```cpp +/** + * 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) {} + * }; + */ +class Solution { +public: + ListNode *removeElements(ListNode *head, int val) { + // O(n) + while (head != NULL && head->val == val) { + head = head->next; + } + + ListNode *before = NULL; + ListNode *ptr = head; + ListNode *tmp; + + while (ptr != NULL) { + if (ptr->val == val) { + if (before != NULL) { + before->next = ptr->next; + } + + // delete ptr and change ptr to ptr->next + tmp = ptr->next; + delete ptr; + ptr = tmp; + } else { + before = ptr; + ptr = ptr->next; + } + } + return head; + } +}; +``` + +These two are taken from discussions, and they are **not** memory safe. +Recursive solution from the same guy: +```cpp +class Solution { +public: + ListNode *removeElements(ListNode *head, int val) { + // Base situation + if (head == NULL) + return NULL; + // Change head->next by it's val (if no val found, will not be changed) + head->next = removeElements(head->next, val); + // Return head or head->next, depending on the val. + // If matched val, return its next, effectively deleting the node. + return (head->val == val) ? head->next : head; + } +}; +``` + +One pointer from [Discussions](https://leetcode.com/problems/remove-linked-list-elements/discuss/722528/C++-2-solutions:-With-single-pointer-+-With-double-pointers-(Easy-to-understand)/1390186) +```cpp +class Solution { +public: + ListNode *removeElements(ListNode *head, int val) { + while (head != NULL && head->val == val) + head = head->next; + + // He checked NULL here, so he doesn't have to check in while loop + if (head == NULL) + return head; + + ListNode *res = head; + while (head->next != NULL) { + if (head->next->val == val) + head->next = head->next->next; + else + head = head->next; + } + return res; + } +}; +``` + diff --git a/CS notes/pages/Leetcode Reverse-Linked-List.md b/CS notes/pages/Leetcode Reverse-Linked-List.md new file mode 100644 index 0000000..407f623 --- /dev/null +++ b/CS notes/pages/Leetcode Reverse-Linked-List.md @@ -0,0 +1,35 @@ +# Leetcode Reverse-Linked-List + +#### 2022-06-15 22:07 + +--- +##### Algorithms: +#algorithm #recursion +##### Data structures: +#DS #linked_list +##### Difficulty: +#leetcode #coding_problem #difficulty-easy +##### Related topics: +```expander +tag:#linked_list +``` + + + +##### Links: +- [Link to problem]() +___ +### Problem + +#### Examples +```markdown +``` + +#### Constraints + +### Thoughts + +> [!summary] +> This is a #template_remove_me + +### Solution