From beb42d515692c5236e01c208b20316db36d88f4e Mon Sep 17 00:00:00 2001 From: juan Date: Thu, 8 Sep 2022 16:18:59 +0800 Subject: [PATCH] vault backup: 2022-09-08 16:18:59 --- OJ notes/OJ-index.md | 2 +- OJ notes/pages/Breadth First Search.md | 2 +- OJ notes/pages/Elementary Math Problems.md | 7 +- .../pages/Floyd's Cycle Finding Algorithm.md | 4 +- OJ notes/pages/Leetcode Add-Two-Numbers.md | 12 +- ...eetcode Binary-Tree-Postorder-Traversal.md | 2 +- ...etcode Intersection-of-Two-Linked-Lists.md | 152 ++++++++++++++++++ .../pages/Leetcode Linked-List-Cycle-II.md | 14 +- OJ notes/pages/Leetcode Longest-Palindrome.md | 2 +- .../pages/Leetcode Pascal's-Triangle-II.md | 2 +- 10 files changed, 175 insertions(+), 24 deletions(-) create mode 100644 OJ notes/pages/Leetcode Intersection-of-Two-Linked-Lists.md diff --git a/OJ notes/OJ-index.md b/OJ notes/OJ-index.md index d28f688..2465b5b 100644 --- a/OJ notes/OJ-index.md +++ b/OJ notes/OJ-index.md @@ -18,7 +18,7 @@ This is where I store notes about CS #### Collection of problems of same kind ```query -#problem_collection +#problem_collection ``` #### TODO diff --git a/OJ notes/pages/Breadth First Search.md b/OJ notes/pages/Breadth First Search.md index db5d757..2d9fd0c 100644 --- a/OJ notes/pages/Breadth First Search.md +++ b/OJ notes/pages/Breadth First Search.md @@ -12,7 +12,7 @@ > > ##### Difficulty: > -> #CS_analysis #difficulty_ +> #CS*analysis #difficulty* > > ##### Additional tags: diff --git a/OJ notes/pages/Elementary Math Problems.md b/OJ notes/pages/Elementary Math Problems.md index 356d194..9b5679a 100644 --- a/OJ notes/pages/Elementary Math Problems.md +++ b/OJ notes/pages/Elementary Math Problems.md @@ -3,10 +3,10 @@ 2022-09-08 15:23 #math -#algorithm +#algorithm #problem_collection -These are problems solved by re-implementing elementary +These are problems solved by re-implementing elementary math: ### Add @@ -18,5 +18,4 @@ math: ### Multiply - -### Divide \ No newline at end of file +### Divide diff --git a/OJ notes/pages/Floyd's Cycle Finding Algorithm.md b/OJ notes/pages/Floyd's Cycle Finding Algorithm.md index 340c4f1..b6ba41a 100644 --- a/OJ notes/pages/Floyd's Cycle Finding Algorithm.md +++ b/OJ notes/pages/Floyd's Cycle Finding Algorithm.md @@ -46,7 +46,7 @@ While traversing the linked list one of these things will occur- - Move the fast pointer by **two** positions. - If both pointers meet at some point then a loop exists and if the fast pointer meets the end position then no loop exists. -Example: +Example: [[Leetcode Linked-List-Cycle]] #### Part 2. **Locating the start** of the loop @@ -98,7 +98,7 @@ Let us consider an example: - Move one step at a time until they met. - The start of the loop is where they met -Example: +Example: [[Leetcode Linked-List-Cycle-II]] #### Example diff --git a/OJ notes/pages/Leetcode Add-Two-Numbers.md b/OJ notes/pages/Leetcode Add-Two-Numbers.md index d909c4f..449130d 100644 --- a/OJ notes/pages/Leetcode Add-Two-Numbers.md +++ b/OJ notes/pages/Leetcode Add-Two-Numbers.md @@ -8,7 +8,7 @@ > > ##### Data structures: > -> #DS #linked_list +> #DS #linked_list > > ##### Difficulty: > @@ -16,7 +16,7 @@ > > ##### Additional tags: > -> #leetcode +> #leetcode > > ##### Revisions: > @@ -62,9 +62,9 @@ You may assume the two numbers do not contain any leading zero, except the numbe #### Constraints -- The number of nodes in each linked list is in the range `[1, 100]`. -- `0 <= Node.val <= 9` -- It is guaranteed that the list represents a number that does not have leading zeros. +- The number of nodes in each linked list is in the range `[1, 100]`. +- `0 <= Node.val <= 9` +- It is guaranteed that the list represents a number that does not have leading zeros. ### Thoughts @@ -132,4 +132,4 @@ public: return ans; } }; -``` \ No newline at end of file +``` diff --git a/OJ notes/pages/Leetcode Binary-Tree-Postorder-Traversal.md b/OJ notes/pages/Leetcode Binary-Tree-Postorder-Traversal.md index 83ccf9a..01bcb9e 100644 --- a/OJ notes/pages/Leetcode Binary-Tree-Postorder-Traversal.md +++ b/OJ notes/pages/Leetcode Binary-Tree-Postorder-Traversal.md @@ -12,7 +12,7 @@ > > ##### Difficulty: > -> #coding_problem #difficulty_ +> #coding*problem #difficulty* > > ##### Additional tags: > diff --git a/OJ notes/pages/Leetcode Intersection-of-Two-Linked-Lists.md b/OJ notes/pages/Leetcode Intersection-of-Two-Linked-Lists.md new file mode 100644 index 0000000..9d240c0 --- /dev/null +++ b/OJ notes/pages/Leetcode Intersection-of-Two-Linked-Lists.md @@ -0,0 +1,152 @@ +# Leetcode Intersection-of-Two-Linked-Lists + +2022-09-08 16:13 + +> ##### Algorithms: +> +> #algorithm #two_pointers +> +> ##### Data structures: +> +> #DS #linked_list #set +> +> ##### Difficulty: +> +> #coding_problem #difficulty-easy +> +> ##### Additional tags: +> +> #leetcode +> +> ##### Revisions: +> +> N/A + +##### Links: + +- [Link to problem](https://leetcode.com/problems/intersection-of-two-linked-lists/) + +--- + +### Problem + +Given the heads of two singly linked-lists `headA` and `headB`, return _the node at which the two lists intersect_. If the two linked lists have no intersection at all, return `null`. + +For example, the following two linked lists begin to intersect at node `c1`: + +![](https://assets.leetcode.com/uploads/2021/03/05/160_statement.png) + +The test cases are generated such that there are no cycles anywhere in the entire linked structure. + +**Note** that the linked lists must **retain their original structure** after the function returns. + +**Custom Judge:** + +The inputs to the **judge** are given as follows (your program is **not** given these inputs): + +- `intersectVal` - The value of the node where the intersection occurs. This is `0` if there is no intersected node. +- `listA` - The first linked list. +- `listB` - The second linked list. +- `skipA` - The number of nodes to skip ahead in `listA` (starting from the head) to get to the intersected node. +- `skipB` - The number of nodes to skip ahead in `listB` (starting from the head) to get to the intersected node. + +The judge will then create the linked structure based on these inputs and pass the two heads, `headA` and `headB` to your program. If you correctly return the intersected node, then your solution will be **accepted**. + +#### Examples + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png) + +**Input:** intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 +**Output:** Intersected at '8' +**Explanation:** The intersected node's value is 8 (note that this must not be 0 if the two lists intersect). +From the head of A, it reads as [4,1,8,4,5]. From the head of B, it reads as [5,6,1,8,4,5]. There are 2 nodes before the intersected node in A; There are 3 nodes before the intersected node in B. + +- Note that the intersected node's value is not 1 because the nodes with value 1 in A and B (2nd node in A and 3rd node in B) are different node references. In other words, they point to two different locations in memory, while the nodes with value 8 in A and B (3rd node in A and 4th node in B) point to the same location in memory. + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png) + +**Input:** intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 +**Output:** Intersected at '2' +**Explanation:** The intersected node's value is 2 (note that this must not be 0 if the two lists intersect). +From the head of A, it reads as [1,9,1,2,4]. From the head of B, it reads as [3,2,4]. There are 3 nodes before the intersected node in A; There are 1 node before the intersected node in B. + +**Example 3:** + +![](https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png) + +**Input:** intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 +**Output:** No intersection +**Explanation:** From the head of A, it reads as [2,6,4]. From the head of B, it reads as [1,5]. Since the two lists do not intersect, intersectVal must be 0, while skipA and skipB can be arbitrary values. +Explanation: The two lists do not intersect, so return null. + +#### Constraints + +- The number of nodes of `listA` is in the `m`. +- The number of nodes of `listB` is in the `n`. +- `1 <= m, n <= 3 * 104` +- `1 <= Node.val <= 105` +- `0 <= skipA < m` +- `0 <= skipB < n` +- `intersectVal` is `0` if `listA` and `listB` do not intersect. +- `intersectVal == listA[skipA] == listB[skipB]` if `listA` and `listB` intersect. + +### Thoughts + +> [!summary] +> This can be solved using #two_pointers or #set + +#### Set + +Record the address of visited node iterating over `listA`, +and verify if the address has been visited when iterating +over `listB` + +#### Two pointers + +#TODO Use two pointers method to solve + +### Solution + +#### Set + +```cpp +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode(int x) : val(x), next(NULL) {} + * }; + */ +class Solution { +public: + ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { + // O(M + N), using set. + unordered_set used; + + ListNode *ptrA = headA; + + while (ptrA) { + used.insert(ptrA); + ptrA = ptrA->next; + } + + ListNode *ptrB = headB; + + while (ptrB) { + if (used.find(ptrB) != used.end()) { + // intersect + return ptrB; + } + + ptrB = ptrB->next; + } + + return nullptr; + } +}; +``` diff --git a/OJ notes/pages/Leetcode Linked-List-Cycle-II.md b/OJ notes/pages/Leetcode Linked-List-Cycle-II.md index 22ea0f1..6f63081 100644 --- a/OJ notes/pages/Leetcode Linked-List-Cycle-II.md +++ b/OJ notes/pages/Leetcode Linked-List-Cycle-II.md @@ -4,11 +4,11 @@ > ##### Algorithms: > -> #algorithm #Floyd_s_cycle_finding_algorithm +> #algorithm #Floyd_s_cycle_finding_algorithm > > ##### Data structures: > -> #DS #linked_list +> #DS #linked_list > > ##### Difficulty: > @@ -16,7 +16,7 @@ > > ##### Additional tags: > -> #leetcode +> #leetcode > > ##### Revisions: > @@ -64,9 +64,9 @@ There is a cycle in a linked list if there is some node in the list that can be #### Constraints -- The number of the nodes in the list is in the range `[0, 104]`. -- `-105 <= Node.val <= 105` -- `pos` is `-1` or a **valid index** in the linked-list. +- The number of the nodes in the list is in the range `[0, 104]`. +- `-105 <= Node.val <= 105` +- `pos` is `-1` or a **valid index** in the linked-list. ### Thoughts @@ -124,4 +124,4 @@ public: } } }; -``` \ No newline at end of file +``` diff --git a/OJ notes/pages/Leetcode Longest-Palindrome.md b/OJ notes/pages/Leetcode Longest-Palindrome.md index 09fc8e9..fad1bd8 100644 --- a/OJ notes/pages/Leetcode Longest-Palindrome.md +++ b/OJ notes/pages/Leetcode Longest-Palindrome.md @@ -30,7 +30,7 @@ ### Problem -Given a string `s` which consists of lowercase or uppercase letters, return *the length of the **longest palindrome*** that can be built with those letters. +Given a string `s` which consists of lowercase or uppercase letters, return \*the length of the **longest palindrome\*** that can be built with those letters. Letters are **case sensitive**, for example, `"Aa"` is not considered a palindrome here. diff --git a/OJ notes/pages/Leetcode Pascal's-Triangle-II.md b/OJ notes/pages/Leetcode Pascal's-Triangle-II.md index fedc908..a558f75 100644 --- a/OJ notes/pages/Leetcode Pascal's-Triangle-II.md +++ b/OJ notes/pages/Leetcode Pascal's-Triangle-II.md @@ -61,7 +61,7 @@ In **Pascal's triangle**, each number is the sum of the two numbers directly abo > [!summary] > This is a #recursion problem. -> +> > Follow up of [[Leetcode Pascal's-Triangle]] Because to get the nth row, we have to get the `n - 1` th