id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
154
<p>Suppose an array of length <code>n</code> sorted in ascending order is <strong>rotated</strong> between <code>1</code> and <code>n</code> times. For example, the array <code>nums = [0,1,4,4,5,6,7]</code> might become:</p> <ul> <li><code>[4,5,6,7,0,1,4]</code> if it was rotated <code>4</code> times.</li> <li><code>[0,1,4,4,5,6,7]</code> if it was rotated <code>7</code> times.</li> </ul> <p>Notice that <strong>rotating</strong> an array <code>[a[0], a[1], a[2], ..., a[n-1]]</code> 1 time results in the array <code>[a[n-1], a[0], a[1], a[2], ..., a[n-2]]</code>.</p> <p>Given the sorted rotated array <code>nums</code> that may contain <strong>duplicates</strong>, return <em>the minimum element of this array</em>.</p> <p>You must decrease the overall operation steps as much as possible.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> nums = [1,3,5] <strong>Output:</strong> 1 </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> nums = [2,2,2,0,1] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 5000</code></li> <li><code>-5000 &lt;= nums[i] &lt;= 5000</code></li> <li><code>nums</code> is sorted and rotated between <code>1</code> and <code>n</code> times.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> This problem is similar to&nbsp;<a href="https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/" target="_blank">Find Minimum in Rotated Sorted Array</a>, but&nbsp;<code>nums</code> may contain <strong>duplicates</strong>. Would this affect the runtime complexity? How and why?</p> <p>&nbsp;</p>
0
{ "code": "class Solution {\npublic:\n int findMin(vector<int>& nums) {\n int n = nums.size();\n if(n==1) {\n return nums[0];\n }\n if(n==2) {\n return min(nums[0], nums[1]);\n }\n \n if(nums[0] < nums[n-1]) {\n return nums[0];\n }\n\n int ans = min(nums[0], nums[n-1]);\n\n int l = 0;\n int r = n-1;\n\n while(l < r && nums[l] == nums[r]) {\n l++;\n r--;\n }\n\n ans = min({ans, nums[l], nums[r]});\n\n if(l > r) {\n return ans;\n }\n\n int left = l+1;\n int right = r-1;\n\n while(left <= right) {\n int mid = left + (right-left) / 2;\n if(nums[mid] < nums[mid-1] && nums[mid] <= nums[mid+1]) {\n ans = min(nums[mid], ans);\n break;\n }\n if(nums[mid] >= nums[mid-1] && nums[mid] > nums[mid+1]) {\n // cout<<nums[mid+1]<<endl;\n ans = min(ans, nums[mid+1]);\n break;\n }\n \n if(nums[mid] >= nums[mid-1] && nums[mid] <= nums[mid+1] && nums[mid] <= nums[l] && nums[mid] <= nums[r]) {\n right = mid-1;\n } else {\n left = mid+1;\n }\n // cout<<left<<\" \"<<right<<endl;\n }\n\n return ans;\n }\n};", "memory": "14700" }
154
<p>Suppose an array of length <code>n</code> sorted in ascending order is <strong>rotated</strong> between <code>1</code> and <code>n</code> times. For example, the array <code>nums = [0,1,4,4,5,6,7]</code> might become:</p> <ul> <li><code>[4,5,6,7,0,1,4]</code> if it was rotated <code>4</code> times.</li> <li><code>[0,1,4,4,5,6,7]</code> if it was rotated <code>7</code> times.</li> </ul> <p>Notice that <strong>rotating</strong> an array <code>[a[0], a[1], a[2], ..., a[n-1]]</code> 1 time results in the array <code>[a[n-1], a[0], a[1], a[2], ..., a[n-2]]</code>.</p> <p>Given the sorted rotated array <code>nums</code> that may contain <strong>duplicates</strong>, return <em>the minimum element of this array</em>.</p> <p>You must decrease the overall operation steps as much as possible.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> nums = [1,3,5] <strong>Output:</strong> 1 </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> nums = [2,2,2,0,1] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 5000</code></li> <li><code>-5000 &lt;= nums[i] &lt;= 5000</code></li> <li><code>nums</code> is sorted and rotated between <code>1</code> and <code>n</code> times.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> This problem is similar to&nbsp;<a href="https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/" target="_blank">Find Minimum in Rotated Sorted Array</a>, but&nbsp;<code>nums</code> may contain <strong>duplicates</strong>. Would this affect the runtime complexity? How and why?</p> <p>&nbsp;</p>
0
{ "code": "class Solution {\npublic:\n\n\n int findMin(vector<int>& nums) {\n int low = 0, high = nums.size() - 1;\n int res = nums[0];\n while (low <= high) {\n int pivot = low + (high - low) / 2;\n res = min(res, nums[pivot]);\n if (nums[pivot] < nums[high])\n high = pivot - 1;\n else if (nums[pivot] > nums[high])\n low = pivot + 1;\n else\n high -= 1;\n }\n return res;\n }\n};", "memory": "14800" }
154
<p>Suppose an array of length <code>n</code> sorted in ascending order is <strong>rotated</strong> between <code>1</code> and <code>n</code> times. For example, the array <code>nums = [0,1,4,4,5,6,7]</code> might become:</p> <ul> <li><code>[4,5,6,7,0,1,4]</code> if it was rotated <code>4</code> times.</li> <li><code>[0,1,4,4,5,6,7]</code> if it was rotated <code>7</code> times.</li> </ul> <p>Notice that <strong>rotating</strong> an array <code>[a[0], a[1], a[2], ..., a[n-1]]</code> 1 time results in the array <code>[a[n-1], a[0], a[1], a[2], ..., a[n-2]]</code>.</p> <p>Given the sorted rotated array <code>nums</code> that may contain <strong>duplicates</strong>, return <em>the minimum element of this array</em>.</p> <p>You must decrease the overall operation steps as much as possible.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> nums = [1,3,5] <strong>Output:</strong> 1 </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> nums = [2,2,2,0,1] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 5000</code></li> <li><code>-5000 &lt;= nums[i] &lt;= 5000</code></li> <li><code>nums</code> is sorted and rotated between <code>1</code> and <code>n</code> times.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> This problem is similar to&nbsp;<a href="https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/" target="_blank">Find Minimum in Rotated Sorted Array</a>, but&nbsp;<code>nums</code> may contain <strong>duplicates</strong>. Would this affect the runtime complexity? How and why?</p> <p>&nbsp;</p>
0
{ "code": "class Solution {\npublic:\n int findMin(vector<int>& nums) {\n int start = 0;\n int end = nums.size()-1;\n int ans = 0;\n while(start<end){\n int mid = start + (end-start)/2;\n if(nums[start]==nums[mid] && nums[mid]==nums[end]){\n start++;\n end--;\n }\n else if(mid>0 && nums[mid]<nums[mid-1]){\n return nums[mid];\n \n }\n else if(nums[mid]<=nums[end]){\n end = mid-1;\n }\n else start = mid+1;\n } \n return nums[start];\n }\n};", "memory": "14900" }
154
<p>Suppose an array of length <code>n</code> sorted in ascending order is <strong>rotated</strong> between <code>1</code> and <code>n</code> times. For example, the array <code>nums = [0,1,4,4,5,6,7]</code> might become:</p> <ul> <li><code>[4,5,6,7,0,1,4]</code> if it was rotated <code>4</code> times.</li> <li><code>[0,1,4,4,5,6,7]</code> if it was rotated <code>7</code> times.</li> </ul> <p>Notice that <strong>rotating</strong> an array <code>[a[0], a[1], a[2], ..., a[n-1]]</code> 1 time results in the array <code>[a[n-1], a[0], a[1], a[2], ..., a[n-2]]</code>.</p> <p>Given the sorted rotated array <code>nums</code> that may contain <strong>duplicates</strong>, return <em>the minimum element of this array</em>.</p> <p>You must decrease the overall operation steps as much as possible.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> nums = [1,3,5] <strong>Output:</strong> 1 </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> nums = [2,2,2,0,1] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 5000</code></li> <li><code>-5000 &lt;= nums[i] &lt;= 5000</code></li> <li><code>nums</code> is sorted and rotated between <code>1</code> and <code>n</code> times.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> This problem is similar to&nbsp;<a href="https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/" target="_blank">Find Minimum in Rotated Sorted Array</a>, but&nbsp;<code>nums</code> may contain <strong>duplicates</strong>. Would this affect the runtime complexity? How and why?</p> <p>&nbsp;</p>
3
{ "code": "class Solution {\npublic:\n int findMin(vector<int>& a) {\n sort(a.begin(),a.end());\n return a[0];\n }\n};", "memory": "15500" }
154
<p>Suppose an array of length <code>n</code> sorted in ascending order is <strong>rotated</strong> between <code>1</code> and <code>n</code> times. For example, the array <code>nums = [0,1,4,4,5,6,7]</code> might become:</p> <ul> <li><code>[4,5,6,7,0,1,4]</code> if it was rotated <code>4</code> times.</li> <li><code>[0,1,4,4,5,6,7]</code> if it was rotated <code>7</code> times.</li> </ul> <p>Notice that <strong>rotating</strong> an array <code>[a[0], a[1], a[2], ..., a[n-1]]</code> 1 time results in the array <code>[a[n-1], a[0], a[1], a[2], ..., a[n-2]]</code>.</p> <p>Given the sorted rotated array <code>nums</code> that may contain <strong>duplicates</strong>, return <em>the minimum element of this array</em>.</p> <p>You must decrease the overall operation steps as much as possible.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> nums = [1,3,5] <strong>Output:</strong> 1 </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> nums = [2,2,2,0,1] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 5000</code></li> <li><code>-5000 &lt;= nums[i] &lt;= 5000</code></li> <li><code>nums</code> is sorted and rotated between <code>1</code> and <code>n</code> times.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> This problem is similar to&nbsp;<a href="https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/" target="_blank">Find Minimum in Rotated Sorted Array</a>, but&nbsp;<code>nums</code> may contain <strong>duplicates</strong>. Would this affect the runtime complexity? How and why?</p> <p>&nbsp;</p>
3
{ "code": "class Solution {\npublic:\n int findMin(vector<int>& nums) {\n sort(nums.begin(), nums.end());\n return nums[0];\n }\n};", "memory": "15500" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\n static const int __ = [](){\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 0;\n}();\n\nint init = [] {\n ofstream out(\"user.out\");\n cout.rdbuf(out.rdbuf());\n for (string str; getline(cin, str); cout << '\\n') {\n int ans = stoi(str);\n for (int i = 0; i < 4; ++i) cin.ignore(numeric_limits<streamsize>::max(), '\\n');\n if (ans) cout << \"Intersected at \\'\" << ans << '\\'';\n else cout << \"No intersection\";\n }\n exit(0);\n return 0;\n}();\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n return nullptr;\n }\n};", "memory": "7300" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\n\n\n /* This is one of the solution possible::\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n if(headA == NULL || headB == NULL) return NULL;\n ListNode* A = headA;\n ListNode* B = headB;\n int pa = 0, pb = 0;\n while (A != NULL) {\n pa++;\n A = A->next;\n }\n while (B != NULL) {\n pb++;\n B = B->next;\n }\n \n A = headA;\n B = headB;\n int n= abs(pa - pb);\n if(pa > pb) {\n while(n--)\n A=A->next;\n }\n else{\n while(n--){\n B = B->next; \n }\n } \n while(A!=NULL){\n if(A == B) return A;\n A=A->next;\n B=B->next;\n }\n return NULL;\n\n }\n};\n*/\nstatic const int __ = [](){\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 0;\n}();\n\nint init = [] {\n ofstream out(\"user.out\");\n cout.rdbuf(out.rdbuf());\n for (string str; getline(cin, str); cout << '\\n') {\n int ans = stoi(str);\n for (int i = 0; i < 4; ++i) cin.ignore(numeric_limits<streamsize>::max(), '\\n');\n if (ans) cout << \"Intersected at \\'\" << ans << '\\'';\n else cout << \"No intersection\";\n }\n exit(0);\n return 0;\n}();\n\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n ListNode* a =headA;\n ListNode* b = headB;\n while (a != b) {\n \n a = (a == NULL) ? headB : a->next;\n \n b = (b == NULL) ? headA : b->next;\n }\n return a;\n }\n};", "memory": "7400" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\n int init = []\n{\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::ofstream out(\"user.out\");\n for(string s; getline(std::cin, s);)\n {\n if(s[0] != '0') out << \"Intersected at '\" << s << \"'\\n\";\n else out << \"No intersection\\n\";\n for(int i = 0; i < 4; ++i) getline(std::cin, s);\n }\n out.flush();\n exit(0);\n return 0;\n}();\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n int lA=0;\n int lB=0;\n ListNode* curr=headA;\n while(curr!=NULL){\n curr=curr->next;\n lA++;\n }\n curr=headB;\n while(curr!=NULL){\n curr=curr->next;\n lB++;\n }\n if(lA<lB){\n int count=lB-lA;\n ListNode* currA=headA;\n ListNode* currB=headB;\n while(currA!=NULL&&currB!=NULL){\n if(currA==currB) return currA;\n if(count!=0) count--;\n else currA=currA->next;\n cout<<count<<endl;\n currB=currB->next;\n }\n }\n else{\n int count=lA-lB;\n ListNode* currA=headA;\n ListNode* currB=headB;\n while(currA!=NULL&&currB!=NULL){\n if(currA==currB) return currA;\n if(count!=0) count--;\n else currB=currB->next;\n currA=currA->next;\n }\n }\n return NULL;\n }\n};", "memory": "7500" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nint init = [] {\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::ofstream out(\"user.out\");\n for (string s; getline(std::cin, s);) {\n if (s[0] != '0')\n out << \"Intersected at '\" << s << \"'\\n\";\n else\n out << \"No intersection\\n\";\n for (int i = 0; i < 4; ++i)\n getline(std::cin, s);\n }\n out.flush();\n exit(0);\n return 0;\n}();\nclass Solution {\npublic:\n ListNode* getIntersectionNode(ListNode* headA, ListNode* headB) {\n unordered_set<ListNode*> s;\n while (headA != nullptr) {\n s.insert(headA);\n headA = headA->next;\n }\n while (headB != nullptr) {\n if (s.find(headB) != s.end())\n return headB;\n headB = headB->next;\n }\n return nullptr;\n }\n};", "memory": "7600" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
0
{ "code": " int init = []\n{\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::ofstream out(\"user.out\");\n for(string s; getline(std::cin, s);)\n {\n if(s[0] != '0') out << \"Intersected at '\" << s << \"'\\n\";\n else out << \"No intersection\\n\";\n for(int i = 0; i < 4; ++i) getline(std::cin, s);\n }\n out.flush();\n exit(0);\n return 0;\n}();\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n ListNode* temp=headA;\n unordered_map<ListNode*,int> mp;\n while(temp){\n mp[temp]++;\n temp=temp->next;\n }\n temp=headB;\n while(temp){\n if(mp[temp]>0)return temp;\n temp=temp->next;\n }\n return nullptr;\n }\n};", "memory": "7700" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\n int init = []\n{\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::ofstream out(\"user.out\");\n for(string s; getline(std::cin, s);)\n {\n if(s[0] != '0') out << \"Intersected at '\" << s << \"'\\n\";\n else out << \"No intersection\\n\";\n for(int i = 0; i < 4; ++i) getline(std::cin, s);\n }\n out.flush();\n exit(0);\n return 0;\n}();\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n unordered_map<ListNode*,int>mpp;\n for(auto p=headA; p!=NULL; p=p->next){\n mpp[p]=p->val;\n }\n for(auto p=headB; p!=NULL; p=p->next){\n if(mpp.find(p)!=mpp.end()) return p; \n }\n return NULL;\n }\n};", "memory": "7800" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
0
{ "code": " int init = []\n{\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::ofstream out(\"user.out\");\n for(string s; getline(std::cin, s);)\n {\n if(s[0] != '0') out << \"Intersected at '\" << s << \"'\\n\";\n else out << \"No intersection\\n\";\n for(int i = 0; i < 4; ++i) getline(std::cin, s);\n }\n out.flush();\n exit(0);\n return 0;\n}();\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n unordered_map<ListNode*,int>mpp;\n for (auto p = headA ; p!=NULL ; p = p->next){\n mpp[p]=p->val;\n }\n for (auto p = headB ; p!=NULL ; p = p->next){\n if (mpp.find(p)!=mpp.end()) return p;\n }\n return NULL;\n }\n};", "memory": "7900" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
0
{ "code": " int init = []\n{\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::ofstream out(\"user.out\");\n for(string s; getline(std::cin, s);)\n {\n if(s[0] != '0') out << \"Intersected at '\" << s << \"'\\n\";\n else out << \"No intersection\\n\";\n for(int i = 0; i < 4; ++i) getline(std::cin, s);\n }\n out.flush();\n exit(0);\n return 0;\n}();\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n unordered_map<ListNode*,int>mpp;\n for (auto p = headA ; p!=NULL ; p = p->next){\n mpp[p]=p->val;\n }\n for (auto p = headB ; p!=NULL ; p = p->next){\n if (mpp.find(p)!=mpp.end()) return p;\n }\n return NULL;\n }\n};", "memory": "8000" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n if(!headA || !headB)\n return NULL;\n ListNode * temp1=headA;\n ListNode * temp2=headB;\n while(temp1!=temp2){\n if\n (temp1==NULL)\n temp1=headB;\n else temp1=temp1->next;\n if(temp2==NULL)\n temp2=headA;\n else temp2=temp2->next;\n \n }\n return temp1;\n }\n};", "memory": "17000" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n if(!headA || !headB) return NULL;\n ListNode* t1=headA;\n ListNode* t2=headB;\n while(t1!=t2){\n if(!t1) t1=headB;\n else{\n t1=t1->next;\n }\n if(!t2) t2=headA;\n else t2=t2->next;\n }\n return t1;\n }\n};", "memory": "17100" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n int len1=length(headA);\n int len2=length(headB);\n while(len1>len2)\n {\n headA=headA->next;\n len1--;\n }\n while(len2>len1)\n {\n headB=headB->next;\n len2--;\n }\n while(headA!=headB)\n {\n headA=headA->next;\n headB=headB->next;\n }\n return headA;\n }\n int length(ListNode*head)\n {\n int len=0;\n ListNode*temp=head;\n while(temp!=NULL)\n {\n temp=temp->next;\n len++;\n }\n return len;\n }\n};", "memory": "17100" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n ListNode *temp1=headA;\n ListNode *temp2=headB;\n while(temp1!=temp2){\n temp1=temp1->next;\n temp2=temp2->next;\n if(temp1==temp2) return temp1;\n if(temp1==NULL){\n temp1=headB;\n }\n if(temp2==NULL){\n temp2=headA;\n }\n\n }\n return temp1;\n }\n};", "memory": "17200" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n if(headA == headB) return headA;\n ListNode* curr1=headA;\n ListNode* curr2=headB;\n int count=0;\n while(curr1->next != curr2->next){\n if(count==2) return nullptr;\n curr1=curr1->next;\n curr2=curr2->next;\n if(curr1 == nullptr) {\n curr1=headB;\n count++;\n }\n if(curr2 == nullptr) curr2=headA;\n if(curr1 == curr2) return curr1;\n }\n\n return curr1->next;\n }\n};", "memory": "17200" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* ans;\n void traverse(ListNode* headA,ListNode* headB){\n if(!headA){while(headB){if(headB->val==0){ans=headB;break;}headB=headB->next;}return;}\n int x=headA->val;\n headA->val=0;\n traverse(headA->next,headB);\n headA->val=x;\n }\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n ans=NULL;\n traverse(headA,headB);\n return ans;\n }\n};", "memory": "17900" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n ListNode*t1=headA;\n ListNode*temp=headA;\n ListNode*t2=headB;\n vector<int>v;\n while(t1!=nullptr){\n v.push_back(t1->val);\n t1->val=-1;\n t1=t1->next;\n }\n while(t2!=nullptr){\n if(t2->val==-1){\n break;\n }\n t2=t2->next;\n }\n int i=0;\n while(temp!=nullptr){\n temp->val=v[i];\n i++;\n temp=temp->next;\n }\n if(t2==nullptr){\n return nullptr;\n }\n return t2;\n }\n};", "memory": "18000" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n ListNode* h1 = headA;\n ListNode* h2 = headB;\n int c1=0,c2=0;\n stack<int> s1,s2;\n while(h1){\n s1.push(h1->val);\n h1=h1->next;\n c1++;\n }\n\n while(h2){\n s2.push(h2->val);\n h2=h2->next;\n c2++;\n }\n\n while(!s1.empty() && !s2.empty()){\n if(s1.top() == s2.top()){\n s1.pop();\n s2.pop();\n c1--;\n c2--;\n }\n else{\n break;\n }\n }\n\n // if(c1!=0){\n for(int i=0;i<c1;i++) headA=headA->next;\n // return headA;\n // }\n // else {\n for(int i=0;i<c2;i++) headB=headB->next;\n // return headB;\n // }\n\n while(headA && headB){\n if(headA==headB) return headA;\n headA = headA->next;\n headB = headB->next;\n }\n\n return headA;\n }\n};", "memory": "18100" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* getIntersectionNode(ListNode* headA, ListNode* headB) {\n ListNode* temp = headA;\n vector<ListNode*> vec;\n while (temp) {\n vec.push_back(temp);\n temp = temp->next;\n }\n temp = headB;\n while (temp) {\n for (auto& iter : vec) {\n if (iter == temp)\n return iter;\n }\n temp = temp->next;\n }\n return nullptr;\n }\n};", "memory": "18200" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n \n stack< ListNode*> s1 ;\n stack< ListNode*> s2 ;\n ListNode *startA = headA, *startB = headB;\n \n while(headA != NULL ){\n s1.push(headA);\n headA=headA->next;\n }\n while(headB != NULL ){\n s2.push(headB);\n headB=headB->next;\n }\n \n \n bool isCommonNode = true ;\n \n ListNode *commonNode = NULL ;\n \n while(isCommonNode && !s1.empty() && !s2.empty()){\n \n ListNode *topA = s1.top();\n ListNode *topB = s2.top() ;\n \n // cout<<s1.top()->val << \" \"<<s2.top()-> val<<endl;\n \n if( topA != topB ) {\n isCommonNode = false ;\n \n }\n else {\n commonNode = topA;\n }\n s1.pop(); s2.pop();\n }\n \n return commonNode;\n \n }\n};", "memory": "18300" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
3
{ "code": "// write a cpp program to fins the intersection os the linked_list\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n vector<ListNode*>adress;\n while(headA!=NULL){\n adress.push_back(headA);\n headA=headA->next;\n }\n int k=0;\n while(headB!=NULL){\n for(int i=0;i<adress.size();i++){\n if(adress[i]==headB){\n return headB;\n }\n }\n headB=headB->next;\n }\n return NULL;\n }\n};", "memory": "18300" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
3
{ "code": "class Solution {\npublic:\n ListNode* getIntersectionNode(ListNode* headA, ListNode* headB) {\n vector<ListNode*> v;\n ListNode* p = headB;\n while (p) {\n v.push_back(p);\n p = p->next;\n }\n\n p = headA;\n\n int size = v.size();\n while (p) {\n for (int i = 0; i < size; i++) {\n if (p == v[i]) return p;\n }\n p = p->next;\n }\n\n return nullptr;\n }\n};", "memory": "18400" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* getIntersectionNode(ListNode* headA, ListNode* headB) {\n ListNode* curr = new ListNode(0);\n curr->next = headA;\n while (curr->next) {\n ListNode* curr2 = new ListNode(0);\n curr2->next = headB;\n while (curr2->next) {\n if (curr->next == curr2->next) {\n if (curr->next != NULL)\n return curr->next;\n }\n curr2 = curr2->next;\n }\n curr = curr->next;\n }\n return NULL;\n }\n};", "memory": "18500" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n std::vector<ListNode*> pAvec;\n ListNode * pA=headA;\n ListNode * pB=headB;\n if(pA==NULL || pB==NULL)\n return NULL;\n while(pA!=NULL)\n {\n // add pA to the vec\n pAvec.push_back(pA);\n pA=pA->next;\n }\n while(pB!=NULL)\n {\n if(std::find(pAvec.begin(),pAvec.end(),pB)!=pAvec.end())\n {\n //find the intersection\n return pB;\n }\n pB=pB->next;\n }\n return NULL;\n }\n};", "memory": "18600" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\n\nclass Solution {\npublic:\nListNode* getIntersectionNode(ListNode* headA, ListNode* headB) {\n stack<ListNode*> a;\n stack<ListNode*> b;\n\n ListNode* p = headA;\n ListNode* q = headB;\n\n while (p != nullptr) {\n a.push(p);\n p = p->next;\n }\n\n while (q != nullptr) {\n b.push(q);\n q = q->next;\n }\n\n ListNode* intersection = nullptr;\n while (!a.empty() && !b.empty()) {\n p = a.top(); q = b.top();\n a.pop(); b.pop();\n\n if (p == q) {\n intersection = p;\n } else {\n break;\n }\n }\n return intersection;\n}\n\n};", "memory": "18700" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n ListNode* h1 = new ListNode(0,headA);\n ListNode* h2 = new ListNode(0,headB);\n\n while(h1!=nullptr){\n h2 = new ListNode(0,headB);\n while(h2!=nullptr){\n if(h1->next == h2->next){\n return h1->next;\n }\n h2 = h2->next;\n }\n h1 = h1->next;\n }\n return NULL;\n\n }\n};", "memory": "18800" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nint len(ListNode *head){\n int c=0;\n while(head!=NULL){\n c++;\n head=head->next;\n }\n return c;\n}\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n int l2=len(headB);\n ListNode *c1=headA;\n ListNode *c2=headB;\n int l1=0;\n while(c1!=NULL){\n ListNode * n1=c1->next;\n ListNode* temp=new ListNode(0);\n c1->next=temp;\n temp->next=n1;\n c1=n1;\n \n }\n int f2=len(headB);\n // cout<<l2<<endl;\n // int k=1;\n\n if(f2==l2){\n ListNode* prev=headA;\n ListNode* curr=prev->next;\n while(curr!=NULL){\n ListNode* next=curr->next;\n prev->next=next;\n curr->next=NULL;\n prev=next;\n if(prev==NULL){\n break;\n }\n curr=prev->next;\n\n }\n return NULL;\n }\n while(c2->next->val!=0){\n \n ListNode * n2=c2->next;\n \n c2=n2;\n \n }\n ListNode* prev=headA;\n ListNode* curr=prev->next;\n while(curr!=NULL){\n ListNode* next=curr->next;\n prev->next=next;\n curr->next=NULL;\n prev=next;\n if(prev==NULL){\n break;\n }\n curr=prev->next;\n\n }\n \n return c2;\n \n }\n};", "memory": "18900" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n vector<ListNode*> firstPath;\n vector<ListNode*> secondPath;\n \n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n while(headA){\n firstPath.push_back(headA);\n headA = headA->next;\n }\n \n while(headB){\n secondPath.push_back(headB);\n headB = headB->next;\n }\n \n int indexA = firstPath.size() - 1, indexB = secondPath.size() - 1;\n if(firstPath[indexA] != secondPath[indexB])\n return nullptr;\n \n while(indexA >= 0 && indexB >= 0 && firstPath[indexA] == secondPath[indexB]){\n indexA--, indexB--;\n }\n \n return firstPath[indexA+1];\n }\n};", "memory": "19400" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n vector<ListNode *> a;\n vector<ListNode *> b;\n ListNode *nodeA = headA;\n ListNode *nodeB = headB;\n while (nodeA != nullptr)\n {\n a.push_back(nodeA);\n nodeA = nodeA->next;\n }\n while (nodeB != nullptr)\n {\n b.push_back(nodeB);\n nodeB = nodeB->next;\n }\n int i = a.size() - 1;\n int j = b.size() - 1;\n if (a[i] != b[j])\n {\n return NULL;\n }\n while (i >= 0 && j >= 0 && a[i] == b[j])\n {\n i--;\n j--;\n }\n return a[i + 1];\n }\n};", "memory": "19500" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\ntypedef ListNode Node;\n#include<vector>\nint notin(long long l,const std::vector<Node*>& arr,Node* n){\n int t=1;\n for(long long i=0;i<l;i++){\n if(arr[i]==n){\n t=0;\n break;\n }\n }\n return t;\n}\n\nclass Solution {\npublic:\n Node *getIntersectionNode(Node *a, Node *b) {\n long long l=0;\n std::vector<Node*> arr;\n while(a!=nullptr || b!=nullptr){\n if(a!= nullptr){\n if(notin(l,arr,a)){\n arr.push_back(a);\n a = a->next;\n l++;\n }\n else\n return a;\n }\n if(b!= nullptr){\n if(notin(l,arr,b)){\n arr.push_back(b);\n b = b->next;\n l++;\n }\n else\n return b;\n }\n }\n return NULL;\n\n }\n};", "memory": "19600" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) \n {\n if(headA==headB)\n return headA;\n vector<ListNode*>a;\n vector<ListNode*>b;\n ListNode* p=headA;\n ListNode* q=headB; \n while(p!=NULL)\n {\n a.push_back(p);\n p=p->next;\n }\n while(q!=NULL)\n {\n b.push_back(q);\n q=q->next;\n }\n int i=a.size()-1,j=b.size()-1;\n while( i>=0 && j>=0)\n {\n if(a[i]==b[j])\n {\n i--;j--;\n }\n else \n {\n if(i==a.size()-1 && j==b.size()-1)\n return NULL;\n if(a[i+1]==b[j+1])\n {\n return a[i+1];\n }\n i--;j--;\n }\n }\n if(a[i+1]==b[j+1])\n {\n return a[i+1];\n }\n return NULL;\n \n }\n};", "memory": "19700" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n set<ListNode*>m;\n\n ListNode* temp1 = headA;\n while(temp1){\n m.insert(temp1);\n temp1 = temp1->next;\n }\n ListNode* temp2 = headB;\n while(temp2){\n if(m.find(temp2)!=m.end()){\n return temp2;\n }\n temp2 = temp2->next;\n }\n return 0;\n }\n};", "memory": "20800" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n if(headA==NULL||headB==NULL)return NULL;\n ListNode*temp1=headA;\n ListNode*temp2=headB;\n\n set<ListNode*>st;\n while(temp1){\n st.insert(temp1);\n temp1=temp1->next;\n }\n while(temp2){\n if(st.find(temp2)!=st.end())return temp2;\n temp2=temp2->next;\n }\n return NULL;\n }\n};", "memory": "20800" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n ListNode *curr1=headA;\n ListNode *curr2=headB;\n unordered_set<ListNode *>mp;\n\n while(curr1!=NULL){\n mp.insert(curr1);\n curr1=curr1->next;\n }\n\n while(curr2!=NULL){\n if(mp.find(curr2)!=mp.end()){\n return curr2;\n }\n curr2=curr2->next;\n }\n return NULL;\n }\n};", "memory": "20900" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
3
{ "code": "class Solution {\n public:\n ListNode* getIntersectionNode(ListNode* headA, ListNode* headB) {\n std::unordered_set<ListNode*> nodes;\n ListNode* a = headA;\n while (a != nullptr) {\n nodes.insert(a);\n a = a->next;\n }\n ListNode* b = headB;\n while (b != nullptr) {\n if (nodes.find(b) != nodes.end()) {\n return b; \n }\n b = b->next;\n }\n \n return nullptr; \n }\n};", "memory": "20900" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n std::unordered_set<ListNode*> h;\n for (auto ha = headA; ha != nullptr; ha = ha->next) {\n h.insert(ha);\n }\n for (auto hb = headB; hb != nullptr; hb = hb->next) {\n if (h.contains(hb)) {\n return hb;\n }\n }\n return nullptr;\n }\n};", "memory": "21000" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n unordered_set<ListNode*> st;\n ListNode* temp=headA;\n while(temp!=NULL){\n st.insert(temp);\n temp=temp->next;\n }\n temp=headB;\n while(temp!=NULL){\n if(st.find(temp)!=st.end())\n break;\n temp=temp->next;\n }\n return temp;\n }\n};", "memory": "21000" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
3
{ "code": "#include <unordered_set>\n\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n std::unordered_set<ListNode*> nodesInA;\n \n // Traverse the first list and add all nodes to the set\n while (headA) {\n nodesInA.insert(headA);\n headA = headA->next;\n }\n \n // Traverse the second list and check if any node is in the set\n while (headB) {\n if (nodesInA.find(headB) != nodesInA.end()) {\n return headB; // Intersection found\n }\n headB = headB->next;\n }\n \n return nullptr; // No intersection\n }\n};\n", "memory": "21100" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n unordered_set<ListNode*> pointer_set;\n ListNode* node = headA;\n while(node) {\n pointer_set.insert(node);\n node = node->next;\n }\n\n ListNode* result = nullptr;\n node = headB;\n while(node) {\n if (pointer_set.contains(node))\n return node;\n node = node->next;\n }\n\n return result;\n }\n};", "memory": "21100" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* getIntersectionNode(ListNode* headA, ListNode* headB) {\n unordered_set<ListNode*> st;\n ListNode *h1 = headA, *h2 = headB;\n while (h1 != NULL) {\n st.insert(h1);\n h1 = h1->next;\n }\n while (h2 != NULL) {\n if (st.find(h2) != st.end())\n return h2;\n h2 = h2->next;\n }\n return NULL;\n }\n};", "memory": "21200" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n unordered_set<ListNode*> hashset;\n\n while (headA) {\n hashset.insert(headA);\n headA = headA->next;\n }\n\n while (headB) {\n if (hashset.find(headB) != hashset.end()) {\n return headB;\n }\n headB = headB->next;\n }\n\n return NULL;\n }\n};", "memory": "21200" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\n #pragma GCC target(\"tune=native\")\n#pragma GCC optimize(\"Ofast\")\n//#pragma GCC optimize(\"Os\")\n// Code here is optimized for size\n\nstatic const auto fastIO = []() {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return 0;\n}();\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n unordered_set<ListNode*>set;\n while(headA){\n set.insert(headA);\n headA=headA->next;\n }\n while(headB){\n if(set.count(headB)) return headB;\n headB=headB->next;\n }\n return nullptr;\n }\n};", "memory": "21300" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n unordered_set<ListNode*> st;\n while(headA){\n st.insert(headA);\n headA=headA->next;\n }\n while(headB){\n if(st.count(headB)) return headB;\n headB=headB->next;\n }\n return NULL;\n }\n};\nauto init=[](){\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n return 'c';\n}();", "memory": "21400" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n unordered_map<ListNode* , int> mpp;\n ListNode* temp1 = headA;\n while(temp1 != NULL){\n mpp[temp1] = 1;\n temp1=temp1->next;\n }\n ListNode* temp2 = headB;\n while(temp2!= NULL){\n if(mpp.find(temp2)!= mpp.end()){\n return temp2;\n }\n temp2=temp2->next;\n }\n return NULL;\n }\n};", "memory": "21500" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n unordered_map<ListNode*,bool>m;\n while(headA!=nullptr){\n m[headA]=true;\n headA=headA->next;\n }\n while(headB!=nullptr){\n if(m.find(headB)!=m.end())return headB;\n headB=headB->next;\n }\n return nullptr;\n }\n};", "memory": "21600" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n map<ListNode*, int>m;\n\n ListNode* temp1 = headA;\n while(temp1){\n m[temp1]++;\n temp1 = temp1->next;\n }\n ListNode* temp2 = headB;\n while(temp2){\n if(m.find(temp2)!=m.end()){\n return temp2;\n }\n temp2 = temp2->next;\n }\n return 0;\n }\n};", "memory": "21700" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n ListNode* tempA = headA;\n ListNode* tempB = headB;\n unordered_map<ListNode*,int>mpp;\n int index = 1;\n while(tempA){\n mpp[tempA]=index;\n index++;\n tempA = tempA->next;\n }\n int final_index =-1;\n while(tempB){\n if(mpp.find(tempB)!=mpp.end()){\n return tempB;\n }\n else{\n tempB = tempB->next;\n }\n }\n return NULL;\n }\n};", "memory": "21800" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n ListNode* tempA = headA;\n ListNode* tempB = headB;\n unordered_map<ListNode*,int>mpp;\n int index = 1;\n while(tempA){\n mpp[tempA]=index;\n index++;\n tempA = tempA->next;\n }\n int final_index =-1;\n while(tempB){\n if(mpp.find(tempB)!=mpp.end()){\n return tempB;\n }\n else{\n tempB = tempB->next;\n }\n }\n return NULL;\n }\n};", "memory": "21800" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n unordered_map<ListNode*,int> m;\n ListNode *temp = headA;\n while(temp != NULL){\n m[temp]++;\n temp=temp->next;\n }\n temp = headB;\n while(temp != NULL){\n if(m.find(temp) != m.end()){\n return temp;\n }\n temp=temp->next;\n }\n return NULL;\n }\n};", "memory": "21900" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n map<ListNode *,int> mp;\n ListNode *temp=headA;\n while(temp!=NULL){\n mp[temp]=1;\n temp=temp->next;\n }\n temp=headB;\n while(temp!=NULL){\n if(mp.find(temp)!=mp.end()){\n return temp;\n }\n temp=temp->next;\n }\n return nullptr;\n }\n};", "memory": "21900" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n map<ListNode*,int> mpp;\n ListNode*temp=headA;\n while(temp!=NULL){\n mpp[temp]=1;\n temp=temp->next;\n }\n temp=headB;\n while(temp!=NULL){\n if(mpp.find(temp)!=mpp.end()) return temp;\n temp=temp->next;\n\n }\n return NULL;\n }\n};", "memory": "22000" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n map<ListNode*,int> mp;\n ListNode* temp1 = headA;\n ListNode* temp2 = headB;\n while(temp1 != NULL) {\n mp[temp1] = 1;\n temp1 = temp1->next;\n }\n while(temp2 != NULL) {\n if(mp.count(temp2)) return temp2;\n temp2 = temp2->next;\n }\n return NULL;\n }\n};", "memory": "22000" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n unordered_set<ListNode*>seen;\n\n ListNode* slow = headA;\n ListNode* fast = headB;\n\n while(slow != NULL || fast != NULL){\n if(slow != NULL){\n if(seen.find(slow) != seen.end()) return slow;\n seen.insert(slow);\n slow = slow->next;\n }\n\n if(fast != NULL){\n if(seen.find(fast) != seen.end()) return fast;\n seen.insert(fast);\n fast = fast->next;\n }\n }\n\n return NULL;\n }\n};", "memory": "23400" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n unordered_set<ListNode*> mp;\n\n ListNode* ptr1 = headA;\n ListNode* ptr2 = headB;\n\n while(ptr1 != NULL && ptr2 != NULL){\n if(mp.find(ptr1) != mp.end()){\n return ptr1;\n }else{\n mp.insert(ptr1);\n }\n\n if(mp.find(ptr2) != mp.end()){\n return ptr2;\n }else{\n mp.insert(ptr2);\n }\n\n ptr1 = ptr1->next;\n ptr2 = ptr2->next;\n }\n\n while(ptr1 != NULL){\n if(mp.find(ptr1) != mp.end()){\n return ptr1;\n }else{\n mp.insert(ptr1);\n }\n ptr1 = ptr1->next;\n }\n\n while(ptr2 != NULL){\n if(mp.find(ptr2) != mp.end()){\n return ptr2;\n }else{\n mp.insert(ptr2);\n }\n ptr2 = ptr2->next;\n }\n\n return NULL;\n }\n};", "memory": "23500" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n if(headA==headB)return headA;\n if(!headA)return headB;\n if(!headB)return headA;\n unordered_set<ListNode*>s;\n while(headA || headB)\n {\n if(s.find(headA)!=s.end())\n return headA;\n if(headA)\n {\n s.insert(headA);\n headA=headA->next;\n }\n if(s.find(headB)!=s.end())\n return headB;\n if(headB)\n {\n s.insert(headB);\n headB=headB->next;\n }\n }\n return NULL;\n }\n};", "memory": "23600" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n unordered_set<ListNode*> s;\n\n ListNode* temp = headA;\n ListNode* temp1 = headB;\n\n while(temp!=NULL){\n // if(s.find(temp) != s.end()){\n // cout<<\"present\";\n // }\n s.insert(temp);\n temp = temp->next;\n }\n while(temp1!=NULL){\n if(s.find(temp1) != s.end()){\n return temp1;\n }\n s.insert(temp1); \n temp1 = temp1->next;\n }\n\n return temp;\n }\n};", "memory": "23600" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n\n if(headA == headB) return headA;\n\n unordered_set<ListNode*> my_s;\n while(headA || headB){\n\n if(headA){\n if(my_s.count(headA)) return headA;\n my_s.insert(headA);\n headA = headA->next;\n }\n\n if(headB){\n if(my_s.count(headB)) return headB;\n my_s.insert(headB);\n headB = headB->next;\n }\n\n }\n \n\n return NULL;\n }\n};", "memory": "23700" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n void fillSet(ListNode *headA, std::set<ListNode*> &s)\n {\n s.insert(headA);\n if (headA)\n fillSet(headA->next, s);\n }\n\n ListNode *searchSet(ListNode *headB, std::set<ListNode*> &s)\n {\n if (s.find(headB) != s.end())\n return headB;\n return searchSet(headB->next, s);\n }\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB)\n {\n std::set<ListNode*> s;\n fillSet(headA, s);\n return searchSet(headB, s);\n }\n};", "memory": "23800" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n set<ListNode*> list;\n while(headA != NULL || headB != NULL){\n if(headA){\n if(list.find(headA) != list.end())\n return headA;\n list.insert(headA);\n headA = headA->next;\n }\n if(headB){\n if(list.find(headB) != list.end())\n return headB;\n list.insert(headB);\n headB = headB->next;\n }\n }\n return NULL;\n }\n};", "memory": "23900" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n set<ListNode *>st;\n ListNode *curr=headA;\n while(curr){\n st.insert(curr);\n curr=curr->next;\n }\n curr=headB;\n while(curr){\n if(st.count(curr)){\n return curr;\n }\n st.insert(curr);\n curr=curr->next;\n }\n return NULL;\n }\n};", "memory": "24000" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n std::set<ListNode *> a;\n std::set<ListNode *> b;\n int flag = 0;\n while (!flag){\n a.insert(headA);\n b.insert(headB);\n if (a.find(headB) != a.end()){\n return headB;\n } else if (b.find(headA) != b.end()){\n return headA;\n } else {\n if (headA != NULL){\n headA = headA->next;\n }\n if (headB != NULL){\n headB = headB->next;\n }\n }\n\n if(headA == NULL && headB == NULL){\n flag = 1;\n }\n }\n return nullptr;\n }\n};", "memory": "24100" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n set<ListNode*> mpp1;\n set<ListNode*> mpp2;\n while(headA!=NULL && headB!=NULL)\n {\n mpp1.insert(headA);\n mpp2.insert(headB);\n if(mpp1.find(headB)!=mpp1.end()) return headB;\n if(mpp2.find(headA)!=mpp2.end()) return headA;\n headA=headA->next;\n headB=headB->next;\n }\n while(headA!=NULL)\n {\n mpp1.insert(headA);\n if(mpp2.find(headA)!=mpp2.end()) return headA;\n headA=headA->next;\n \n }\n while(headB!=NULL)\n {\n mpp2.insert(headB);\n if(mpp1.find(headB)!=mpp1.end()) return headB;\n headB=headB->next;\n \n }\n return nullptr;\n \n }\n};", "memory": "24200" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n // 透過記憶體位址來確認是否指向同一個 node\n //STEP 1 : 尋訪 A List,紀錄每個node的記憶體位址\n unordered_map<int,vector<void*>> table;\n\n while(headA != NULL){\n table[headA->val].push_back(&headA[0]);\n headA = headA -> next;\n }\n \n //STEP 2 : 尋訪 B List,找到相同的記憶體位址及返回其值\n while(headB != NULL){\n if(table.find(headB->val) != table.end()){\n for(int i=0; i<table[headB->val].size(); i++){\n if(table[headB->val][i] == &headB[0])\n return headB;\n }\n }\n headB = headB -> next;\n }\n\n return nullptr; \n }\n};", "memory": "24300" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n int counta,countb;\n ListNode*curr1=headA;\n ListNode*curr2=headB;\n\n map<ListNode*,int>m;\n\n \n while(curr1!=NULL){\n m[curr1]=1;\n curr1=curr1->next;\n }\n while(curr2!=NULL){\n if(m[curr2]==1){\n return curr2;\n }\n m[curr2]=1;\n curr2=curr2->next;\n }\n return NULL;\n\n\n }\n};", "memory": "24700" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n map<ListNode*,int> mpp;\n ListNode* t1 = headA;\n ListNode* t2 = headB; \n\n while(t1!=NULL && t2!=NULL ){\n if(mpp.find(t1) != mpp.end()){\n return t1;\n }\n mpp[t1] = 1;\n t1 = t1->next;\n if(mpp.find(t2) != mpp.end()){\n return t2;\n }\n mpp[t2] = 1;\n t2 = t2->next;\n }\n while(t1==NULL && t2!=NULL){\n if(mpp.find(t2) != mpp.end()){\n return t2;\n }\n mpp[t2] = 1;\n t2 = t2->next;\n }\n while(t2==NULL && t1!=NULL){\n if(mpp.find(t1) != mpp.end()){\n return t1;\n }\n mpp[t1] = 1;\n t1 = t1->next;\n }\n \n return nullptr;\n }\n};", "memory": "24800" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n map<ListNode*,bool> m;\n ListNode* temp=headA;\n ListNode* temp22=NULL;\n while(temp!=NULL)\n {\n m[temp]=1;\n temp=temp->next;\n }\n temp=headB;\n while(temp!=NULL)\n {\n if(m[temp])\n return temp;\n temp=temp->next;\n }\n return temp22;\n \n }\n};", "memory": "24900" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n unordered_map<ListNode* ,bool>visited;\n\n while(headA!=NULL){\n visited[headA]=true;\n headA=headA->next;\n }\n\n while(headB!=NULL){\n if(visited[headB])\n return headB;\n headB=headB->next;\n }\n return NULL;\n }\n};", "memory": "24900" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code>:</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_statement.png" style="width: 500px; height: 162px;" /> <p>The test cases are generated such that there are no cycles anywhere in the entire linked structure.</p> <p><strong>Note</strong> that the linked lists must <strong>retain their original structure</strong> after the function returns.</p> <p><strong>Custom Judge:</strong></p> <p>The inputs to the <strong>judge</strong> are given as follows (your program is <strong>not</strong> given these inputs):</p> <ul> <li><code>intersectVal</code> - The value of the node where the intersection occurs. This is <code>0</code> if there is no intersected node.</li> <li><code>listA</code> - The first linked list.</li> <li><code>listB</code> - The second linked list.</li> <li><code>skipA</code> - The number of nodes to skip ahead in <code>listA</code> (starting from the head) to get to the intersected node.</li> <li><code>skipB</code> - The number of nodes to skip ahead in <code>listB</code> (starting from the head) to get to the intersected node.</li> </ul> <p>The judge will then create the linked structure based on these inputs and pass the two heads, <code>headA</code> and <code>headB</code> to your program. If you correctly return the intersected node, then your solution will be <strong>accepted</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png" style="width: 500px; height: 162px;" /> <pre> <strong>Input:</strong> intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 <strong>Output:</strong> Intersected at &#39;8&#39; <strong>Explanation:</strong> The intersected node&#39;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&#39;s value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> 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 (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png" style="width: 500px; height: 194px;" /> <pre> <strong>Input:</strong> intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 <strong>Output:</strong> Intersected at &#39;2&#39; <strong>Explanation:</strong> The intersected node&#39;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. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png" style="width: 300px; height: 189px;" /> <pre> <strong>Input:</strong> intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 <strong>Output:</strong> No intersection <strong>Explanation:</strong> 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. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes of <code>listA</code> is in the <code>m</code>.</li> <li>The number of nodes of <code>listB</code> is in the <code>n</code>.</li> <li><code>1 &lt;= m, n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= skipA &lt;&nbsp;m</code></li> <li><code>0 &lt;= skipB &lt;&nbsp;n</code></li> <li><code>intersectVal</code> is <code>0</code> if <code>listA</code> and <code>listB</code> do not intersect.</li> <li><code>intersectVal == listA[skipA] == listB[skipB]</code> if <code>listA</code> and <code>listB</code> intersect.</li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you write a solution that runs in <code>O(m + n)</code> time and use only <code>O(1)</code> memory?
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n unordered_map<ListNode*,int> checker;\n\n while(headA != nullptr){\n checker[headA]++;\n headA = headA->next;\n }\n\n while(headB != nullptr){\n if(checker[headB] > 0){\n return headB;\n }\n headB = headB->next;\n }\n return NULL;\n }\n};", "memory": "25000" }
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbsp;<strong>any number of times</strong>.</p> <p>Return the&nbsp;lexicographically smallest string that <code>s</code>&nbsp;can be changed to after using the swaps.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2]] <strong>Output:</strong> &quot;bacd&quot; <strong>Explaination:</strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[1] and s[2], s = &quot;bacd&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2],[0,2]] <strong>Output:</strong> &quot;abcd&quot; <strong>Explaination: </strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[0] and s[2], s = &quot;acbd&quot; Swap s[1] and s[2], s = &quot;abcd&quot;</pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;cba&quot;, pairs = [[0,1],[1,2]] <strong>Output:</strong> &quot;abc&quot; <strong>Explaination: </strong> Swap s[0] and s[1], s = &quot;bca&quot; Swap s[1] and s[2], s = &quot;bac&quot; Swap s[0] and s[1], s = &quot;abc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs[i][0], pairs[i][1] &lt;&nbsp;s.length</code></li> <li><code>s</code>&nbsp;only contains lower case English letters.</li> </ul>
0
{ "code": "/*\n[0, 2] [2, 4] [0, 5]\nzybvxf\nbfz\n\nif we can group indices which are kind of connected, and then can sort the characters on those indices and if \nwe do this for every group we will get the lexicographically smallest string.\n*/\n\nstruct DSU {\n vector<int> par, rank;\n \n DSU(int n) {\n par = vector<int>(n);\n rank = vector<int>(n);\n for (int i = 0; i < n; ++i) {\n par[i] = i, rank[i] = 1;\n }\n }\n \n int findPar(int u) {\n if (par[u] == u) {\n return u;\n }\n return par[u] = findPar(par[u]);\n }\n \n void doUnion(int u, int v) {\n u = findPar(u), v = findPar(v);\n if (rank[u] > rank[v]) {\n par[v] = u;\n }\n else if (rank[v] > rank[u]) {\n par[u] = v;\n }\n else {\n par[u] = v;\n rank[v] += 1;\n }\n }\n};\n\nclass Solution {\npublic:\n \n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int n = s.size();\n DSU dsu(n);\n \n for (auto &pair: pairs) {\n int u = pair[0], v = pair[1];\n if (dsu.findPar(u) != dsu.findPar(v)) {\n dsu.doUnion(u, v);\n }\n }\n \n unordered_map<int, string> indGroup;\n for (int i = 0; i < n; ++i) {\n indGroup[dsu.findPar(i)].push_back(s[i]);\n }\n for (auto &[parInd, charGroup] : indGroup) {\n sort(charGroup.begin(), charGroup.end(), greater<char>());\n }\n \n string resStr = \"\";\n for (int i = 0; i < n; ++i) {\n resStr.push_back(indGroup[dsu.findPar(i)].back());\n indGroup[dsu.findPar(i)].pop_back();\n }\n return resStr;\n }\n};", "memory": "50562" }
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbsp;<strong>any number of times</strong>.</p> <p>Return the&nbsp;lexicographically smallest string that <code>s</code>&nbsp;can be changed to after using the swaps.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2]] <strong>Output:</strong> &quot;bacd&quot; <strong>Explaination:</strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[1] and s[2], s = &quot;bacd&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2],[0,2]] <strong>Output:</strong> &quot;abcd&quot; <strong>Explaination: </strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[0] and s[2], s = &quot;acbd&quot; Swap s[1] and s[2], s = &quot;abcd&quot;</pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;cba&quot;, pairs = [[0,1],[1,2]] <strong>Output:</strong> &quot;abc&quot; <strong>Explaination: </strong> Swap s[0] and s[1], s = &quot;bca&quot; Swap s[1] and s[2], s = &quot;bac&quot; Swap s[0] and s[1], s = &quot;abc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs[i][0], pairs[i][1] &lt;&nbsp;s.length</code></li> <li><code>s</code>&nbsp;only contains lower case English letters.</li> </ul>
0
{ "code": "class UnionFind {\npublic:\n UnionFind(int sz) : root(sz), rank(sz), count(sz) {\n for (int i = 0; i < sz; i++) {\n root[i] = i;\n rank[i] = 1;\n }\n }\n\n int find(int x) {\n if (x == root[x]) {\n return x;\n }\n\t// Some ranks may become obsolete so they are not updated\n return root[x] = find(root[x]);\n }\n\n void unionSet(int x, int y) {\n int rootX = find(x);\n int rootY = find(y);\n if (rootX != rootY) {\n if (rank[rootX] > rank[rootY]) {\n root[rootY] = rootX;\n } else if (rank[rootX] < rank[rootY]) {\n root[rootX] = rootY;\n } else {\n root[rootY] = rootX;\n rank[rootX] += 1;\n }\n count--;\n }\n }\n\n bool connected(int x, int y) {\n return find(x) == find(y);\n }\n \n int getCount(void) {\n return count;\n }\n\nprivate:\n vector<int> root;\n vector<int> rank;\n int count;\n};\n\nclass Solution {\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int n = s.length();\n UnionFind uf(n);\n \n for (vector<int> &p : pairs) {\n uf.unionSet(p[0], p[1]);\n }\n \n unordered_map<int,string> m;\n \n for (int i = 0; i < n; i++) {\n int root = uf.find(i);\n m[root] += s[i];\n }\n \n if (uf.getCount() == 1) {\n sort(s.begin(), s.end());\n return s;\n }\n \n \n for (auto pairs : m) {\n int i = pairs.first;\n string s = pairs.second;\n \n sort(s.begin(), s.end(), greater<char>());\n \n m[i] = s;\n }\n \n for (int i = 0; i < n; i++) {\n int root = uf.find(i);\n s[i]=m[root].back();\n m[root].pop_back();\n }\n \n return s;\n }\n};", "memory": "50562" }
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbsp;<strong>any number of times</strong>.</p> <p>Return the&nbsp;lexicographically smallest string that <code>s</code>&nbsp;can be changed to after using the swaps.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2]] <strong>Output:</strong> &quot;bacd&quot; <strong>Explaination:</strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[1] and s[2], s = &quot;bacd&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2],[0,2]] <strong>Output:</strong> &quot;abcd&quot; <strong>Explaination: </strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[0] and s[2], s = &quot;acbd&quot; Swap s[1] and s[2], s = &quot;abcd&quot;</pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;cba&quot;, pairs = [[0,1],[1,2]] <strong>Output:</strong> &quot;abc&quot; <strong>Explaination: </strong> Swap s[0] and s[1], s = &quot;bca&quot; Swap s[1] and s[2], s = &quot;bac&quot; Swap s[0] and s[1], s = &quot;abc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs[i][0], pairs[i][1] &lt;&nbsp;s.length</code></li> <li><code>s</code>&nbsp;only contains lower case English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int findUPar(int node,vector<int> &parent)\n {\n if(parent[node]==node)return node;\n return parent[node]=findUPar(parent[node],parent);\n }\n void unionBySize(vector<int> &size,int node1,int node2,vector<int> &parent)\n {\n int ulPar1=findUPar(node1,parent);\n int ulPar2=findUPar(node2,parent);\n if(ulPar1==ulPar2)return;\n if(size[ulPar1]>size[ulPar2])\n {\n parent[ulPar2]=ulPar1;\n size[ulPar1]+=size[ulPar2];\n }\n else\n {\n parent[ulPar1]=ulPar2;\n size[ulPar2]+=size[ulPar1];\n }\n }\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int n=s.size();\n vector<int> vis(n,0);\n vector<int> size(n,1);\n vector<int> parent(n,0);\n for(int i=0;i<n;i++)parent[i]=i;\n unordered_map<int,vector<int>> mp;\n for(int i=0;i<pairs.size();i++)\n {\n int node1=pairs[i][0];\n int node2=pairs[i][1];\n unionBySize(size,node1,node2,parent);\n }\n for(int i=0;i<n;i++)\n {\n int ulPar=findUPar(i,parent);\n if(size[ulPar]>1)\n {\n mp[ulPar].push_back(i);\n }\n }\n for(auto &m:mp)\n {\n vector<int> indices=m.second;\n string temp;\n for(auto &idx:indices)\n {\n temp+=s[idx];\n }\n sort(temp.begin(),temp.end());\n int j=0;\n for(auto &idx:indices)\n {\n s[idx]=temp[j];\n j++;\n }\n }\n return s;\n \n }\n};", "memory": "51087" }
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbsp;<strong>any number of times</strong>.</p> <p>Return the&nbsp;lexicographically smallest string that <code>s</code>&nbsp;can be changed to after using the swaps.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2]] <strong>Output:</strong> &quot;bacd&quot; <strong>Explaination:</strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[1] and s[2], s = &quot;bacd&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2],[0,2]] <strong>Output:</strong> &quot;abcd&quot; <strong>Explaination: </strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[0] and s[2], s = &quot;acbd&quot; Swap s[1] and s[2], s = &quot;abcd&quot;</pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;cba&quot;, pairs = [[0,1],[1,2]] <strong>Output:</strong> &quot;abc&quot; <strong>Explaination: </strong> Swap s[0] and s[1], s = &quot;bca&quot; Swap s[1] and s[2], s = &quot;bac&quot; Swap s[0] and s[1], s = &quot;abc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs[i][0], pairs[i][1] &lt;&nbsp;s.length</code></li> <li><code>s</code>&nbsp;only contains lower case English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int findP(int node, vector<int>& parent)\n\t{\n\t if(parent[node]==node)\n\t {\n\t return node;\n\t }\n\t return parent[node]=findP(parent[node], parent);\n\t}\n\t\n\tvoid unionSize (int u, int v, vector<int>& parent, vector<int>& size)\n\t{\n\t int paru=findP(u,parent);\n\t int parv=findP(v,parent);\n\t if(parv==paru)\n\t {\n\t return;\n\t }\n\t if(size[paru]<size[parv])\n\t {\n\t parent[paru]=parv;\n\t size[parv]+=size[paru];\n\t }\n\t else\n\t {\n\t parent[parv]=paru;\n\t size[paru]+=size[parv];\n\t }\n\t}\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n string ans=\"\";\n unordered_map<int, vector<char>>m;\n int n=s.size();\n vector<int>parent(n);\n vector<int>size(n,1);\n for(int i=0;i<n;i++)\n {\n parent[i]=i;\n }\n for(int i=0;i<pairs.size();i++)\n {\n unionSize(pairs[i][0], pairs[i][1], parent, size);\n }\n for(int i=0;i<n;i++)\n {\n int par=findP(i, parent);\n m[par].push_back(s[i]);\n }\n for(auto& val:m)\n {\n sort(val.second.begin(), val.second.end(), greater<char>());\n }\n for(int i=0;i<n;i++)\n {\n int par=findP(i, parent);\n s[i]=m[par].back();\n m[par].pop_back();\n }\n return s;\n }\n};", "memory": "51087" }
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbsp;<strong>any number of times</strong>.</p> <p>Return the&nbsp;lexicographically smallest string that <code>s</code>&nbsp;can be changed to after using the swaps.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2]] <strong>Output:</strong> &quot;bacd&quot; <strong>Explaination:</strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[1] and s[2], s = &quot;bacd&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2],[0,2]] <strong>Output:</strong> &quot;abcd&quot; <strong>Explaination: </strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[0] and s[2], s = &quot;acbd&quot; Swap s[1] and s[2], s = &quot;abcd&quot;</pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;cba&quot;, pairs = [[0,1],[1,2]] <strong>Output:</strong> &quot;abc&quot; <strong>Explaination: </strong> Swap s[0] and s[1], s = &quot;bca&quot; Swap s[1] and s[2], s = &quot;bac&quot; Swap s[0] and s[1], s = &quot;abc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs[i][0], pairs[i][1] &lt;&nbsp;s.length</code></li> <li><code>s</code>&nbsp;only contains lower case English letters.</li> </ul>
0
{ "code": "\nclass UnionFind {\n public:\n UnionFind(int n) : id(n), rank(n) {\n iota(id.begin(), id.end(), 0);\n }\n\n void unionByRank(int u, int v) {\n const int i = find(u);\n const int j = find(v);\n if (i == j)\n return;\n if (rank[i] < rank[j]) {\n id[i] = j;\n } else if (rank[i] > rank[j]) {\n id[j] = i;\n } else {\n id[i] = j;\n ++rank[j];\n }\n }\n\n int find(int u) {\n return id[u] == u ? u : id[u] = find(id[u]);\n }\n\n private:\n vector<int> id;\n vector<int> rank;\n};\n\nclass Solution {\n public:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n string ans;\n UnionFind uf(s.length());\n unordered_map<int, priority_queue<char, vector<char>, greater<>>>\n indexToLetters;\n\n for (const vector<int>& pair : pairs) {\n const int a = pair[0];\n const int b = pair[1];\n uf.unionByRank(a, b);\n }\n\n for (int i = 0; i < s.length(); ++i)\n indexToLetters[uf.find(i)].push(s[i]);\n\n for (int i = 0; i < s.length(); ++i)\n ans += indexToLetters[uf.find(i)].top(), indexToLetters[uf.find(i)].pop();\n\n return ans;\n }\n};", "memory": "51612" }
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbsp;<strong>any number of times</strong>.</p> <p>Return the&nbsp;lexicographically smallest string that <code>s</code>&nbsp;can be changed to after using the swaps.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2]] <strong>Output:</strong> &quot;bacd&quot; <strong>Explaination:</strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[1] and s[2], s = &quot;bacd&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2],[0,2]] <strong>Output:</strong> &quot;abcd&quot; <strong>Explaination: </strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[0] and s[2], s = &quot;acbd&quot; Swap s[1] and s[2], s = &quot;abcd&quot;</pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;cba&quot;, pairs = [[0,1],[1,2]] <strong>Output:</strong> &quot;abc&quot; <strong>Explaination: </strong> Swap s[0] and s[1], s = &quot;bca&quot; Swap s[1] and s[2], s = &quot;bac&quot; Swap s[0] and s[1], s = &quot;abc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs[i][0], pairs[i][1] &lt;&nbsp;s.length</code></li> <li><code>s</code>&nbsp;only contains lower case English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int n = s.size();\n vector<int> parent(n);\n iota(parent.begin(), parent.end(), 0);\n function<int(int)> find = [&](int x) {\n return parent[x] == x ? x : parent[x] = find(parent[x]);\n };\n for (auto& p : pairs) {\n int root1 = find(p[0]);\n int root2 = find(p[1]);\n if (root1 != root2) parent[root2] = root1;\n }\n unordered_map<int, vector<int>> groups;\n for (int i = 0; i < n; ++i) groups[find(i)].push_back(i);\n for (auto& group : groups) {\n string temp;\n for (int idx : group.second) temp += s[idx];\n sort(temp.begin(), temp.end());\n for (int i = 0; i < group.second.size(); ++i) s[group.second[i]] = temp[i];\n }\n return s;\n }\n};", "memory": "51612" }
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbsp;<strong>any number of times</strong>.</p> <p>Return the&nbsp;lexicographically smallest string that <code>s</code>&nbsp;can be changed to after using the swaps.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2]] <strong>Output:</strong> &quot;bacd&quot; <strong>Explaination:</strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[1] and s[2], s = &quot;bacd&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2],[0,2]] <strong>Output:</strong> &quot;abcd&quot; <strong>Explaination: </strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[0] and s[2], s = &quot;acbd&quot; Swap s[1] and s[2], s = &quot;abcd&quot;</pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;cba&quot;, pairs = [[0,1],[1,2]] <strong>Output:</strong> &quot;abc&quot; <strong>Explaination: </strong> Swap s[0] and s[1], s = &quot;bca&quot; Swap s[1] and s[2], s = &quot;bac&quot; Swap s[0] and s[1], s = &quot;abc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs[i][0], pairs[i][1] &lt;&nbsp;s.length</code></li> <li><code>s</code>&nbsp;only contains lower case English letters.</li> </ul>
0
{ "code": "class UniFond{\n int* root;\n int* rank;\n public:\n UniFond(int size){\n root=new int[size];\n rank=new int[size];\n for(int i=0;i<size;i++){\n root[i]=i;\n }\n }\n int find(int node){\n if (node==root[node]) return node;\n int parent=find(root[node]);\n return parent;\n }\n void Union(int node_1, int node_2){\n int root_1=find(node_1);\n int root_2=find(node_2);\n if (root_1==root_2) return;\n \n if(rank[root_1]>rank[root_2]){\n root[root_2]=root_1;\n rank[root_1]++;\n }\n else{\n root[root_1]=root_2;\n rank[root_2]++;\n }\n return;\n }\n \n};\nclass Solution {\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n UniFond uni(s.size());\n for(int i=0;i<pairs.size();i++){\n uni.Union(pairs[i][0], pairs[i][1]);\n }\n unordered_map<int, vector<int>> Tree;//{root, {idx1, idx2}}\n \n for(int i=0;i<s.size();i++){\n int parent=uni.find(i);\n Tree[parent].push_back(i);\n }\n \n for(auto iter=Tree.begin();iter!=Tree.end();iter++){\n string tmp=\"\"; \n for(int idx:iter->second){\n tmp+=s[idx];\n }\n sort(tmp.begin(),tmp.end());\n int i=0;\n for(int idx:iter->second){\n s[idx]=tmp[i++];\n }\n }\n return s;\n \n \n \n \n \n \n \n }\n};", "memory": "52137" }
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbsp;<strong>any number of times</strong>.</p> <p>Return the&nbsp;lexicographically smallest string that <code>s</code>&nbsp;can be changed to after using the swaps.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2]] <strong>Output:</strong> &quot;bacd&quot; <strong>Explaination:</strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[1] and s[2], s = &quot;bacd&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2],[0,2]] <strong>Output:</strong> &quot;abcd&quot; <strong>Explaination: </strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[0] and s[2], s = &quot;acbd&quot; Swap s[1] and s[2], s = &quot;abcd&quot;</pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;cba&quot;, pairs = [[0,1],[1,2]] <strong>Output:</strong> &quot;abc&quot; <strong>Explaination: </strong> Swap s[0] and s[1], s = &quot;bca&quot; Swap s[1] and s[2], s = &quot;bac&quot; Swap s[0] and s[1], s = &quot;abc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs[i][0], pairs[i][1] &lt;&nbsp;s.length</code></li> <li><code>s</code>&nbsp;only contains lower case English letters.</li> </ul>
0
{ "code": "#include <vector>\n#include <string>\n#include <algorithm>\n#include <unordered_map>\nusing namespace std;\n\nclass Solution {\npublic:\n // Union-Find (Disjoint Set Union) Helper functions\n int find(int x, vector<int>& parent) {\n if (parent[x] != x) {\n parent[x] = find(parent[x], parent); // Path compression\n }\n return parent[x];\n }\n \n void unionSet(int x, int y, vector<int>& parent, vector<int>& rank) {\n int rootX = find(x, parent);\n int rootY = find(y, parent);\n \n if (rootX != rootY) {\n if (rank[rootX] > rank[rootY]) {\n parent[rootY] = rootX;\n } else if (rank[rootX] < rank[rootY]) {\n parent[rootX] = rootY;\n } else {\n parent[rootY] = rootX;\n rank[rootX]++;\n }\n }\n }\n \n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int n = s.size();\n vector<int> parent(n);\n vector<int> rank(n, 0);\n \n // Initialize the parent array where each element is its own parent\n for (int i = 0; i < n; ++i) {\n parent[i] = i;\n }\n \n // Union-Find to connect the pairs\n for (const auto& pair : pairs) {\n unionSet(pair[0], pair[1], parent, rank);\n }\n \n // Group the indices by their root parent (connected components)\n unordered_map<int, vector<int>> components;\n for (int i = 0; i < n; ++i) {\n int root = find(i, parent);\n components[root].push_back(i);\n }\n \n // Sort each component's characters and place them back in the string\n for (auto& component : components) {\n vector<int>& indices = component.second;\n string chars;\n \n // Collect characters at the indices of this component\n for (int idx : indices) {\n chars.push_back(s[idx]);\n }\n \n // Sort the characters and the indices\n sort(chars.begin(), chars.end());\n sort(indices.begin(), indices.end());\n \n // Reassign sorted characters to the sorted indices\n for (int i = 0; i < indices.size(); ++i) {\n s[indices[i]] = chars[i];\n }\n }\n \n return s;\n }\n};\n", "memory": "52137" }
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbsp;<strong>any number of times</strong>.</p> <p>Return the&nbsp;lexicographically smallest string that <code>s</code>&nbsp;can be changed to after using the swaps.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2]] <strong>Output:</strong> &quot;bacd&quot; <strong>Explaination:</strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[1] and s[2], s = &quot;bacd&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2],[0,2]] <strong>Output:</strong> &quot;abcd&quot; <strong>Explaination: </strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[0] and s[2], s = &quot;acbd&quot; Swap s[1] and s[2], s = &quot;abcd&quot;</pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;cba&quot;, pairs = [[0,1],[1,2]] <strong>Output:</strong> &quot;abc&quot; <strong>Explaination: </strong> Swap s[0] and s[1], s = &quot;bca&quot; Swap s[1] and s[2], s = &quot;bac&quot; Swap s[0] and s[1], s = &quot;abc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs[i][0], pairs[i][1] &lt;&nbsp;s.length</code></li> <li><code>s</code>&nbsp;only contains lower case English letters.</li> </ul>
0
{ "code": "class DSU {\npublic:\n vector<int> parent;\n vector<int> rank;\n\n DSU(int n) {\n parent.resize(n);\n rank.resize(n, 1);\n for (int i = 0; i < parent.size(); i++) {\n parent[i] = i;\n }\n }\n\n int find(int x) {\n if (x != parent[x]) {\n parent[x] = find(parent[x]);\n }\n return parent[x];\n }\n\n bool unite(int x, int y) {\n int xRoot = find(x);\n int yRoot = find(y);\n if (xRoot == yRoot) {\n return false;\n }\n if (rank[xRoot] > rank[yRoot]) {\n parent[yRoot] = xRoot;\n rank[xRoot] += rank[yRoot];\n } else {\n parent[xRoot] = yRoot;\n rank[yRoot] += rank[xRoot];\n }\n return true;\n }\n};\nclass Solution {\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int n = s.size();\n DSU dsu(n);\n \n for (auto& p : pairs) {\n dsu.unite(p[0], p[1]);\n }\n\n unordered_map<int, vector<int>> components;\n for (int i = 0; i < n; i++) {\n int root = dsu.find(i);\n components[root].push_back(i);\n }\n\n string ans = s;\n for (auto& comp : components) {\n vector<int>& indices = comp.second;\n string temp = \"\";\n \n \n for (int idx : indices) {\n temp += s[idx];\n }\n \n \n sort(temp.begin(), temp.end());\n \n for (int i = 0; i < indices.size(); i++) {\n ans[indices[i]] = temp[i];\n }\n }\n\n return ans;\n }\n};", "memory": "52662" }
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbsp;<strong>any number of times</strong>.</p> <p>Return the&nbsp;lexicographically smallest string that <code>s</code>&nbsp;can be changed to after using the swaps.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2]] <strong>Output:</strong> &quot;bacd&quot; <strong>Explaination:</strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[1] and s[2], s = &quot;bacd&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2],[0,2]] <strong>Output:</strong> &quot;abcd&quot; <strong>Explaination: </strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[0] and s[2], s = &quot;acbd&quot; Swap s[1] and s[2], s = &quot;abcd&quot;</pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;cba&quot;, pairs = [[0,1],[1,2]] <strong>Output:</strong> &quot;abc&quot; <strong>Explaination: </strong> Swap s[0] and s[1], s = &quot;bca&quot; Swap s[1] and s[2], s = &quot;bac&quot; Swap s[0] and s[1], s = &quot;abc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs[i][0], pairs[i][1] &lt;&nbsp;s.length</code></li> <li><code>s</code>&nbsp;only contains lower case English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> rank, par;\n Solution() {\n // Default constructor implementation\n }\n Solution(int n){\n rank = vector<int> (n,1);\n for(int i=0; i<n; i++){\n par.push_back(i);\n }\n }\n int find(int x){\n if(par[x] == x) return x;\n return par[x] = find(par[x]);\n }\n void union_(int x , int y){\n int parx = find(x), pary = find(y);\n if(parx == pary) return;\n if(rank[parx] > rank[pary]){\n par[pary] = parx;\n rank[parx]+=rank[pary];\n }\n else{\n par[parx] = pary;\n rank[pary]+=rank[parx];\n }\n return;\n }\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n Solution ds(s.size());\n map<int, priority_queue<char, vector<char>, greater<char>>> mp;\n for(int i=0; i<pairs.size(); i++){\n ds.union_(pairs[i][0], pairs[i][1]);\n }\n for(int i=0; i<s.size(); i++){\n mp[ds.find(i)].push(s[i]);\n }\n for(int i=0; i<s.size(); i++){\n s[i] = mp[ds.find(i)].top();\n mp[ds.find(i)].pop();\n }\n return s;\n }\n};", "memory": "52662" }
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbsp;<strong>any number of times</strong>.</p> <p>Return the&nbsp;lexicographically smallest string that <code>s</code>&nbsp;can be changed to after using the swaps.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2]] <strong>Output:</strong> &quot;bacd&quot; <strong>Explaination:</strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[1] and s[2], s = &quot;bacd&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2],[0,2]] <strong>Output:</strong> &quot;abcd&quot; <strong>Explaination: </strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[0] and s[2], s = &quot;acbd&quot; Swap s[1] and s[2], s = &quot;abcd&quot;</pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;cba&quot;, pairs = [[0,1],[1,2]] <strong>Output:</strong> &quot;abc&quot; <strong>Explaination: </strong> Swap s[0] and s[1], s = &quot;bca&quot; Swap s[1] and s[2], s = &quot;bac&quot; Swap s[0] and s[1], s = &quot;abc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs[i][0], pairs[i][1] &lt;&nbsp;s.length</code></li> <li><code>s</code>&nbsp;only contains lower case English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n \n int find(int i,vector<int> &parent){\n if(parent[i]==-1) return i;\n return parent[i]=find(parent[i],parent);\n }\n \n void unio(int a,int b,vector<int> &parent){\n int xs=find(a,parent);\n int ys=find(b,parent);\n if(xs!=ys){\n parent[ys]=xs;\n }\n }\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n vector<int> parent(s.size(),-1);\n for(int i=0;i<pairs.size();i++){\n unio(pairs[i][0],pairs[i][1],parent);\n }\n \n unordered_map<int,vector<int> > mp;\n for(int i=0;i<parent.size();i++){\n mp[find(i,parent)].push_back(i);\n }\n \n for(auto m:mp){\n string st=\"\";\n for(auto v:m.second){\n st+=s[v];\n }\n sort(st.begin(),st.end());\n int j=0;\n for(auto i:m.second){\n s[i]=st[j++];\n }\n }\n return s;\n \n }\n};\n", "memory": "53187" }
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbsp;<strong>any number of times</strong>.</p> <p>Return the&nbsp;lexicographically smallest string that <code>s</code>&nbsp;can be changed to after using the swaps.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2]] <strong>Output:</strong> &quot;bacd&quot; <strong>Explaination:</strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[1] and s[2], s = &quot;bacd&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2],[0,2]] <strong>Output:</strong> &quot;abcd&quot; <strong>Explaination: </strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[0] and s[2], s = &quot;acbd&quot; Swap s[1] and s[2], s = &quot;abcd&quot;</pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;cba&quot;, pairs = [[0,1],[1,2]] <strong>Output:</strong> &quot;abc&quot; <strong>Explaination: </strong> Swap s[0] and s[1], s = &quot;bca&quot; Swap s[1] and s[2], s = &quot;bac&quot; Swap s[0] and s[1], s = &quot;abc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs[i][0], pairs[i][1] &lt;&nbsp;s.length</code></li> <li><code>s</code>&nbsp;only contains lower case English letters.</li> </ul>
0
{ "code": "class UnionFind {\npublic:\n UnionFind(const int size) : ranks_(size), parents_(size) {\n std::iota(parents_.begin(), parents_.end(), 0);\n }\n\n void union_set(const int x, const int y) {\n const auto x_parent = find(x);\n const auto y_parent = find(y);\n if (x_parent == y_parent)\n return;\n\n if (ranks_[x_parent] >= ranks_[y_parent]) {\n if (ranks_[x_parent] == ranks_[y_parent]) {\n ++ranks_[x_parent];\n }\n parents_[y_parent] = x_parent; \n } else {\n parents_[x_parent] = y_parent; \n }\n }\n\n int find(const int node) {\n if (parents_[node] == node) {\n return node;\n }\n parents_[node] = find(parents_[node]);\n return parents_[node];\n }\nprivate:\n std::vector<int> ranks_;\n std::vector<int> parents_;\n};\n\nclass Solution {\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n UnionFind uf(s.size());\n for (const auto& pair : pairs) {\n uf.union_set(pair[0], pair[1]);\n }\n std::unordered_map<int, std::vector<int>> components;\n for (auto i = 0; i < s.size(); ++i) {\n components[uf.find(i)].emplace_back(i);\n }\n std::vector<char> ret(s.size());\n for (auto& [_, component] : components) {\n std::vector<char> partial_string(component.size());\n std::ranges::sort(component);\n std::ranges::transform(component, partial_string.begin(), [&s](const int i){return s[i];});\n std::ranges::sort(partial_string);\n for (auto i = 0; i < component.size(); ++i) {\n ret[component[i]] = partial_string[i];\n }\n }\n\n return std::string(ret.begin(), ret.end());\n \n }\n};", "memory": "53187" }
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbsp;<strong>any number of times</strong>.</p> <p>Return the&nbsp;lexicographically smallest string that <code>s</code>&nbsp;can be changed to after using the swaps.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2]] <strong>Output:</strong> &quot;bacd&quot; <strong>Explaination:</strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[1] and s[2], s = &quot;bacd&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2],[0,2]] <strong>Output:</strong> &quot;abcd&quot; <strong>Explaination: </strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[0] and s[2], s = &quot;acbd&quot; Swap s[1] and s[2], s = &quot;abcd&quot;</pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;cba&quot;, pairs = [[0,1],[1,2]] <strong>Output:</strong> &quot;abc&quot; <strong>Explaination: </strong> Swap s[0] and s[1], s = &quot;bca&quot; Swap s[1] and s[2], s = &quot;bac&quot; Swap s[0] and s[1], s = &quot;abc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs[i][0], pairs[i][1] &lt;&nbsp;s.length</code></li> <li><code>s</code>&nbsp;only contains lower case English letters.</li> </ul>
0
{ "code": "class DSU{\npublic:\n vector<int>parent;\n vector<int>size;\n DSU(int n){\n parent.resize(n);\n size.resize(n,1);\n for(int i=0;i<n;i++)\n parent[i]=i;\n }\n int find(int u){\n if(u==parent[u])\n return u;\n return parent[u]=find(parent[u]);\n }\n void unite(int u,int v){\n int pu = find(u);\n int pv =find(v);\n if(pu==pv)\n return;\n if(pu<pv){\n parent[pv]=pu;\n size[pu]+=size[pv];\n }\n else\n {\n parent[pu]=pv;\n size[pv]+=size[pu];\n }\n }\n};\n\nclass Solution {\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int n =s.length();\n DSU ds(n);\n for(int i=0;i<pairs.size();i++)\n ds.unite(pairs[i][0],pairs[i][1]);\n unordered_map<int,vector<int>>mp;\n for(int i=0;i<n;i++){\n int p =ds.find(i);\n mp[p].push_back(i);\n }\n for(auto it:mp){\n string ex=\"\";\n for(auto itt : it.second)\n ex+=s[itt];\n sort(ex.begin(),ex.end());\n int j=0;\n for(auto itt:it.second)\n s[itt]=ex[j++];\n }\n return s;\n }\n};", "memory": "53712" }
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbsp;<strong>any number of times</strong>.</p> <p>Return the&nbsp;lexicographically smallest string that <code>s</code>&nbsp;can be changed to after using the swaps.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2]] <strong>Output:</strong> &quot;bacd&quot; <strong>Explaination:</strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[1] and s[2], s = &quot;bacd&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2],[0,2]] <strong>Output:</strong> &quot;abcd&quot; <strong>Explaination: </strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[0] and s[2], s = &quot;acbd&quot; Swap s[1] and s[2], s = &quot;abcd&quot;</pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;cba&quot;, pairs = [[0,1],[1,2]] <strong>Output:</strong> &quot;abc&quot; <strong>Explaination: </strong> Swap s[0] and s[1], s = &quot;bca&quot; Swap s[1] and s[2], s = &quot;bac&quot; Swap s[0] and s[1], s = &quot;abc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs[i][0], pairs[i][1] &lt;&nbsp;s.length</code></li> <li><code>s</code>&nbsp;only contains lower case English letters.</li> </ul>
0
{ "code": "\n\nclass Solution {\npublic:\nclass DSU {\nprivate:\n vector<int> parent, rank;\n\npublic:\n // Initialize the DSU with n elements\n DSU(int n) {\n parent.resize(n);\n rank.resize(n, 0);\n for (int i = 0; i < n; ++i) {\n parent[i] = i;\n }\n }\n\n // Find the root of the set containing x with path compression\n int find(int x) {\n if (parent[x] != x) {\n parent[x] = find(parent[x]);\n }\n return parent[x];\n }\n\n // Union by rank\n void unionSets(int x, int y) {\n int rootX = find(x);\n int rootY = find(y);\n if (rootX != rootY) {\n if (rank[rootX] > rank[rootY]) {\n parent[rootY] = rootX;\n } else if (rank[rootX] < rank[rootY]) {\n parent[rootX] = rootY;\n } else {\n parent[rootY] = rootX;\n rank[rootX]++;\n }\n }\n }\n};\n\nstring smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int n = s.size();\n DSU dsu(n);\n\n // Union the pairs of indices\n for (const auto& pair : pairs) {\n dsu.unionSets(pair[0], pair[1]);\n }\n\n // Group all characters by their connected component (root)\n unordered_map<int, vector<int>> components; // {root : [indices]}\n for (int i = 0; i < n; ++i) {\n int root = dsu.find(i);\n components[root].push_back(i);\n }\n\n // For each component, sort the characters and place them in the correct order\n string result = s;\n for (const auto& component : components) {\n vector<int> indices = component.second;\n\n // Get all characters in this component and sort them\n string chars;\n for (int idx : indices) {\n chars += s[idx];\n }\n sort(chars.begin(), chars.end());\n\n // Put the sorted characters back into the corresponding indices\n for (int i = 0; i < indices.size(); ++i) {\n result[indices[i]] = chars[i];\n }\n }\n\n return result;\n}\n\n};", "memory": "53712" }
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbsp;<strong>any number of times</strong>.</p> <p>Return the&nbsp;lexicographically smallest string that <code>s</code>&nbsp;can be changed to after using the swaps.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2]] <strong>Output:</strong> &quot;bacd&quot; <strong>Explaination:</strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[1] and s[2], s = &quot;bacd&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2],[0,2]] <strong>Output:</strong> &quot;abcd&quot; <strong>Explaination: </strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[0] and s[2], s = &quot;acbd&quot; Swap s[1] and s[2], s = &quot;abcd&quot;</pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;cba&quot;, pairs = [[0,1],[1,2]] <strong>Output:</strong> &quot;abc&quot; <strong>Explaination: </strong> Swap s[0] and s[1], s = &quot;bca&quot; Swap s[1] and s[2], s = &quot;bac&quot; Swap s[0] and s[1], s = &quot;abc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs[i][0], pairs[i][1] &lt;&nbsp;s.length</code></li> <li><code>s</code>&nbsp;only contains lower case English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> root;\n vector<int> rank;\n int find(int x) {\n if(x == root[x]) return x;\n root[x] = find(root[x]);\n return root[x];\n }\n void Union(int x, int y) {\n int rx = find(x);\n int ry = find(y);\n if(rx!=ry) {\n if(rank[rx] > rank[ry]) root[ry] = rx;\n else if(rank[rx] < rank[ry]) root[rx] = ry;\n else {\n root[rx] = ry;\n rank[ry] += 1;\n }\n }\n }\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int n = s.size();\n for(int i=0;i<n;i++) {\n root.push_back(i);\n rank.push_back(1);\n }\n for(auto & ele : pairs) {\n Union(ele[0], ele[1]);\n }\n // find root of each node\n map<int,vector<int>> children; // {root, all_children} children including root self\n for(int i=0;i<n;i++) children[find(i)].push_back(i);\n\n for (auto & [k, v] : children) {\n string child;\n for(auto idx : v) child.push_back(s[idx]);\n sort(child.begin(), child.end());\n int i = 0;\n for(auto c: child) {\n s[v[i]] = c;\n i++;\n }\n }\n return s;\n }\n};", "memory": "54237" }
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbsp;<strong>any number of times</strong>.</p> <p>Return the&nbsp;lexicographically smallest string that <code>s</code>&nbsp;can be changed to after using the swaps.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2]] <strong>Output:</strong> &quot;bacd&quot; <strong>Explaination:</strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[1] and s[2], s = &quot;bacd&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2],[0,2]] <strong>Output:</strong> &quot;abcd&quot; <strong>Explaination: </strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[0] and s[2], s = &quot;acbd&quot; Swap s[1] and s[2], s = &quot;abcd&quot;</pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;cba&quot;, pairs = [[0,1],[1,2]] <strong>Output:</strong> &quot;abc&quot; <strong>Explaination: </strong> Swap s[0] and s[1], s = &quot;bca&quot; Swap s[1] and s[2], s = &quot;bac&quot; Swap s[0] and s[1], s = &quot;abc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs[i][0], pairs[i][1] &lt;&nbsp;s.length</code></li> <li><code>s</code>&nbsp;only contains lower case English letters.</li> </ul>
0
{ "code": "class Solution {\n class DSU{\n public:\n vector<int> parents;\n vector<int> nodeSize;\n DSU(int n){\n for(int i=0;i<n;i++) parents.push_back(i);\n nodeSize.resize(n,1);\n }\n int findParent(int node){\n if(parents[node]==node) return node;\n int par = findParent(parents[node]);\n parents[node]=par;\n return par;\n }\n void unionBySize(int x,int y){\n int parX = findParent(x);\n int parY = findParent(y);\n if(parX==parY) return;\n if(nodeSize[parX]>=nodeSize[parY]){\n parents[parY]=parX;\n nodeSize[parX]+=nodeSize[parY];\n }else{\n parents[parX]=parY;\n nodeSize[parY]+=nodeSize[parX];\n }\n }\n };\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n DSU jai(s.size());\n for(auto& p:pairs){\n jai.unionBySize(p[0],p[1]);\n }\n unordered_map<int,vector<int>> mp;\n for(int i=0;i<s.size();i++){\n int par = jai.findParent(i);\n mp[par].push_back(i);\n }\n for(auto &p:mp){\n vector<int> idx = p.second;\n sort(idx.begin(),idx.end());\n string curr=\"\";\n for(int i:idx) curr+=s[i];\n sort(curr.begin(),curr.end());\n for(int i=0;i<idx.size();i++) s[idx[i]]=curr[i];\n }\n return s;\n }\n};", "memory": "54237" }
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbsp;<strong>any number of times</strong>.</p> <p>Return the&nbsp;lexicographically smallest string that <code>s</code>&nbsp;can be changed to after using the swaps.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2]] <strong>Output:</strong> &quot;bacd&quot; <strong>Explaination:</strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[1] and s[2], s = &quot;bacd&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2],[0,2]] <strong>Output:</strong> &quot;abcd&quot; <strong>Explaination: </strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[0] and s[2], s = &quot;acbd&quot; Swap s[1] and s[2], s = &quot;abcd&quot;</pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;cba&quot;, pairs = [[0,1],[1,2]] <strong>Output:</strong> &quot;abc&quot; <strong>Explaination: </strong> Swap s[0] and s[1], s = &quot;bca&quot; Swap s[1] and s[2], s = &quot;bac&quot; Swap s[0] and s[1], s = &quot;abc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs[i][0], pairs[i][1] &lt;&nbsp;s.length</code></li> <li><code>s</code>&nbsp;only contains lower case English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\nclass dsu{\n private:\n vector<int> parent;\n vector<int> rank;\n public:\n dsu(int x){\n parent.resize(x);\n rank.resize(x);\n for(int i =0; i < x;i++){\n parent[i] = i;\n rank[i] = 1;\n }\n }\n bool connect(int x, int y){\n int p1 = find(x);\n int p2 = find(y);\n if(p1 != p2){\n if(rank[p1] > rank[p2]){\n parent[p2] = p1;\n } else if(rank[p2] > rank[p1]){\n parent[p1] = p2;\n } else{\n parent[p2] = p1;\n rank[p1]++;\n }\n return true;\n }\n return false;\n }\n int find(int x){\n if(x != parent[x]) parent[x] = find(parent[x]);\n return parent[x];\n }\n};\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n dsu DS(s.size());\n for(auto & pair : pairs){\n DS.connect(pair[0], pair[1]);\n }\n unordered_map<int, vector<int>> adjList;\n for(int i =0; i < s.size(); i++){\n adjList[DS.find(i)].push_back(i);\n }\n for(auto g : adjList){\n vector<char> chars;\n for(int ind : g.second){\n chars.push_back(s[ind]);\n }\n sort(chars.begin(), chars.end());\n sort(g.second.begin(), g.second.end());\n\n for(int i =0; i < g.second.size(); i++){\n s[g.second[i]] = chars[i];\n }\n }\n return s;\n }\n};", "memory": "54762" }
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbsp;<strong>any number of times</strong>.</p> <p>Return the&nbsp;lexicographically smallest string that <code>s</code>&nbsp;can be changed to after using the swaps.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2]] <strong>Output:</strong> &quot;bacd&quot; <strong>Explaination:</strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[1] and s[2], s = &quot;bacd&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2],[0,2]] <strong>Output:</strong> &quot;abcd&quot; <strong>Explaination: </strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[0] and s[2], s = &quot;acbd&quot; Swap s[1] and s[2], s = &quot;abcd&quot;</pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;cba&quot;, pairs = [[0,1],[1,2]] <strong>Output:</strong> &quot;abc&quot; <strong>Explaination: </strong> Swap s[0] and s[1], s = &quot;bca&quot; Swap s[1] and s[2], s = &quot;bac&quot; Swap s[0] and s[1], s = &quot;abc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs[i][0], pairs[i][1] &lt;&nbsp;s.length</code></li> <li><code>s</code>&nbsp;only contains lower case English letters.</li> </ul>
0
{ "code": "class DisJoint{\n public:\n vector<int>parent;\n vector<int>size;\n DisJoint(int n)\n {\n parent.resize(n,0);\n size.resize(n,0);\n for(int i=0;i<n;i++)\n {\n parent[i]=i;\n }\n }\n int findParent(int node)\n {\n if(node==parent[node])\n return node;\n return parent[node]=findParent(parent[node]);\n }\n void bySize(int u,int v)\n {\n int ulu=findParent(u);\n int ulv=findParent(v);\n if(size[ulu]>size[ulv])\n {\n parent[ulv]=ulu;\n size[ulu]+=size[ulv];\n }\n else\n {\n parent[ulu]=ulv;\n size[ulv]+=size[ulu];\n }\n }\n};\nclass Solution {\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int n=s.length();\n DisJoint ds(n);\n for(int i=0;i<pairs.size();i++)\n {\n ds.bySize(pairs[i][0],pairs[i][1]);\n } \n vector<vector<int>>mp(s.length());\n for(int i=0;i<n;i++)\n {\n mp[ds.findParent(i)].push_back(i);\n }\n for(auto it:mp)\n {\n string ss=\"\";\n for(auto ids:it)\n {\n ss+=s[ids];\n }\n sort(ss.begin(),ss.end());\n for(int i=0;i<it.size();i++)\n {\n s[it[i]]=ss[i];\n }\n }\n return s;\n \n }\n};", "memory": "54762" }
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbsp;<strong>any number of times</strong>.</p> <p>Return the&nbsp;lexicographically smallest string that <code>s</code>&nbsp;can be changed to after using the swaps.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2]] <strong>Output:</strong> &quot;bacd&quot; <strong>Explaination:</strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[1] and s[2], s = &quot;bacd&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2],[0,2]] <strong>Output:</strong> &quot;abcd&quot; <strong>Explaination: </strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[0] and s[2], s = &quot;acbd&quot; Swap s[1] and s[2], s = &quot;abcd&quot;</pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;cba&quot;, pairs = [[0,1],[1,2]] <strong>Output:</strong> &quot;abc&quot; <strong>Explaination: </strong> Swap s[0] and s[1], s = &quot;bca&quot; Swap s[1] and s[2], s = &quot;bac&quot; Swap s[0] and s[1], s = &quot;abc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs[i][0], pairs[i][1] &lt;&nbsp;s.length</code></li> <li><code>s</code>&nbsp;only contains lower case English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n \n int find_uparent(int x, vector<int>& parent)\n {\n if(parent[x]==x) return x;\n return parent[x]=find_uparent(parent[x], parent);\n }\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n // find all elements that belong to ne group\n\n //union find implementation\n int str_len = s.size();\n vector<int> parent(str_len);\n vector<int> rank(str_len, 0);\n\n for(int i=0; i<str_len; i++)\n {\n parent[i] = i;\n }\n\n for(int i=0; i<pairs.size(); i++)\n {\n int first = pairs[i][0];\n int second = pairs[i][1];\n\n int p1 = find_uparent(first, parent);\n int p2 = find_uparent(second, parent);\n // return \"\";\n if(p1 != p2)\n {\n if(rank[p1] >= rank[p2])\n {\n parent[p2] = p1;\n rank[p1]+=rank[p2];\n // cout<<\" loop 1 \"<<parent[p2]<<\" \"<<p2;\n }\n else\n {\n parent[p1] =p2;\n rank[p2]+=rank[p1];\n cout<<\" loop 2\"<<parent[p1]<<\" \"<<p2;\n }\n\n }\n \n }\n // return \"\";\n //doing path compression, while doing a find\n \n for(int i=0; i<str_len; i++)\n {\n parent[i] = find_uparent(i, parent);\n cout<<\"i and parent is: \"<<i<<\" \"<<parent[i]<<\" \";\n }\n\n\n // map<int, vector<char>> m1;\n // for(int i=0; i<str_len; i++)\n // {\n // m1[parent[i]].push_back(s[i]);\n // }\n\n // //sort the vector in map\n // string final=\"\";\n // for(auto it: m1)\n // {\n // vector<char> temp = it.second;\n // sort(temp.begin(),temp.end());\n // for(int k=0; k<temp.size(); k++) final+=temp[k];\n // // m1[it.first] = temp;\n // }\n\n // return final;\n\n //we have sorted vectors of the size of map, we need to merge them, merge based off the first element\n\n //we have to do map and sorting, so will use a priority queue\n\n unordered_map<int, priority_queue<int, vector<int>, greater<int>>> m1;\n unordered_map<int, priority_queue<char, vector<char>, greater<char>>> m2;\n\n for(int i=0; i<str_len; i++)\n {\n m1[parent[i]].push(i);\n m2[parent[i]].push(s[i]);\n }\n\n //within connected components sorting done now reassiging the indices\n\n int i=0;\n while(s[i] != '\\0')\n {\n if(m1.find(i) != m1.end())\n {\n while(m1[i].size())\n {\n int top_ind = m1[i].top();\n char top_val = m2[i].top();\n m1[i].pop();\n m2[i].pop();\n cout<<top_ind<<\" \"<<top_val<<\" \";\n s[top_ind] = top_val;\n }\n }\n i++;\n }\n\n return s;\n }\n};", "memory": "55287" }
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbsp;<strong>any number of times</strong>.</p> <p>Return the&nbsp;lexicographically smallest string that <code>s</code>&nbsp;can be changed to after using the swaps.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2]] <strong>Output:</strong> &quot;bacd&quot; <strong>Explaination:</strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[1] and s[2], s = &quot;bacd&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2],[0,2]] <strong>Output:</strong> &quot;abcd&quot; <strong>Explaination: </strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[0] and s[2], s = &quot;acbd&quot; Swap s[1] and s[2], s = &quot;abcd&quot;</pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;cba&quot;, pairs = [[0,1],[1,2]] <strong>Output:</strong> &quot;abc&quot; <strong>Explaination: </strong> Swap s[0] and s[1], s = &quot;bca&quot; Swap s[1] and s[2], s = &quot;bac&quot; Swap s[0] and s[1], s = &quot;abc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs[i][0], pairs[i][1] &lt;&nbsp;s.length</code></li> <li><code>s</code>&nbsp;only contains lower case English letters.</li> </ul>
0
{ "code": "class Disjoint\n{\npublic:\n vector<int> parent,size;\n Disjoint(int n)\n {\n parent.resize(n,0);\n for(int i=0;i<n;i++)\n parent[i]=i;\n size.resize(n,1);\n\n }\n\n int findpar(int u)\n {\n if(parent[u]==u)\n return u;\n else\n {\n return parent[u]=findpar(parent[u]);\n }\n }\n\n void unionsize(int u,int v)\n {\n int upar=findpar(u);int vpar=findpar(v);\n if(upar==vpar)\n return;\n else\n {\n if(size[upar]>size[vpar])\n {\n parent[vpar]=upar;\n size[upar]+=size[vpar];\n }\n else\n {\n parent[upar]=vpar;\n size[vpar]+=size[upar];\n }\n \n }\n }\n};\nclass Solution {\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int n=s.size();\n Disjoint ds(n);\n for(int i=0;i<pairs.size();i++)\n {\n ds.unionsize(pairs[i][0],pairs[i][1]);\n\n }\n\n map<int,vector<int>> mp;\n for(int i=0;i<n;i++)\n {\n int par=ds.findpar(i);\n mp[par].push_back(i);\n }\n string res(n,'*');\n for(auto it:mp)\n {\n vector<int> cur=it.second;\n string temp=\"\";\n for(int c:cur)\n {\n temp+=s[c];\n }\n sort(temp.begin(),temp.end());\n \n for(int i=0;i<cur.size();i++)\n {\n res[cur[i]]=temp[i];\n }\n }\n return res;\n }\n};", "memory": "55287" }
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbsp;<strong>any number of times</strong>.</p> <p>Return the&nbsp;lexicographically smallest string that <code>s</code>&nbsp;can be changed to after using the swaps.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2]] <strong>Output:</strong> &quot;bacd&quot; <strong>Explaination:</strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[1] and s[2], s = &quot;bacd&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2],[0,2]] <strong>Output:</strong> &quot;abcd&quot; <strong>Explaination: </strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[0] and s[2], s = &quot;acbd&quot; Swap s[1] and s[2], s = &quot;abcd&quot;</pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;cba&quot;, pairs = [[0,1],[1,2]] <strong>Output:</strong> &quot;abc&quot; <strong>Explaination: </strong> Swap s[0] and s[1], s = &quot;bca&quot; Swap s[1] and s[2], s = &quot;bac&quot; Swap s[0] and s[1], s = &quot;abc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs[i][0], pairs[i][1] &lt;&nbsp;s.length</code></li> <li><code>s</code>&nbsp;only contains lower case English letters.</li> </ul>
1
{ "code": "class DSU {\nprivate:\n vector<int> parents;\n vector<int> sizes;\npublic:\n DSU(int n) {\n parents.resize(n);\n iota(parents.begin(), parents.end(), 0);\n sizes.assign(n, 1);\n }\n\n int find(int a) {\n if (parents[a] != a) {\n return parents[a] = find(parents[a]);\n }\n return parents[a];\n }\n\n void join(int smaller, int larger) {\n smaller = find(smaller);\n larger = find(larger);\n if (smaller != larger) {\n if (sizes[smaller] > sizes[larger]) {\n swap(smaller, larger);\n }\n sizes[larger] += sizes[smaller];\n parents[smaller] = larger;\n }\n }\n\n vector<vector<int>> getSets() {\n unordered_map<int, vector<int>> sets;\n for (int i = 0; i < parents.size(); ++i) {\n sets[find(i)].push_back(i);\n }\n vector<vector<int>> ret;\n for (auto& kv : sets) {\n ret.push_back(kv.second);\n }\n return ret;\n }\n\n};\nclass Solution {\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int n = pairs.size();\n DSU dsu(s.length());\n for (auto& v : pairs) {\n dsu.join(v[0], v[1]);\n }\n vector<vector<int>> sets = dsu.getSets();\n for (auto& set : sets) {\n vector<char> chars;\n for (int i : set) {\n chars.push_back(s[i]);\n }\n sort(chars.begin(), chars.end());\n sort(set.begin(), set.end());\n for (int i = 0; i < set.size(); ++i) {\n s[set[i]] = chars[i];\n }\n }\n return s;\n }\n};", "memory": "55812" }
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbsp;<strong>any number of times</strong>.</p> <p>Return the&nbsp;lexicographically smallest string that <code>s</code>&nbsp;can be changed to after using the swaps.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2]] <strong>Output:</strong> &quot;bacd&quot; <strong>Explaination:</strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[1] and s[2], s = &quot;bacd&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2],[0,2]] <strong>Output:</strong> &quot;abcd&quot; <strong>Explaination: </strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[0] and s[2], s = &quot;acbd&quot; Swap s[1] and s[2], s = &quot;abcd&quot;</pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;cba&quot;, pairs = [[0,1],[1,2]] <strong>Output:</strong> &quot;abc&quot; <strong>Explaination: </strong> Swap s[0] and s[1], s = &quot;bca&quot; Swap s[1] and s[2], s = &quot;bac&quot; Swap s[0] and s[1], s = &quot;abc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs[i][0], pairs[i][1] &lt;&nbsp;s.length</code></li> <li><code>s</code>&nbsp;only contains lower case English letters.</li> </ul>
1
{ "code": "class Solution {\n class unionFind {\n vector<int> root;\n vector<int> rank;\n \n public:\n unionFind(int sz) : root(sz), rank(sz, 1) {\n for (int i = 0; i < sz; i++) {\n root[i] = i;\n }\n }\n\n int find(int x) {\n if (x == root[x]) return x;\n return root[x] = find(root[x]);\n }\n\n void unionf(int x, int y) {\n int rootx = find(x);\n int rooty = find(y);\n if (rootx != rooty) {\n if (rank[rootx] > rank[rooty]) {\n root[rooty] = rootx;\n } else if (rank[rooty] > rank[rootx]) {\n root[rootx] = rooty;\n } else {\n root[rootx] = rooty;\n rank[rooty] += 1;\n }\n }\n }\n\n vector<vector<int>> connectedComponents(int n) {\n unordered_map<int, vector<int>> components;\n for (int i = 0; i < n; i++) {\n int t = find(i);\n components[t].push_back(i);\n }\n\n vector<vector<int>> res;\n for (auto& comp : components) {\n res.push_back(comp.second);\n }\n return res;\n }\n };\n \npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int n = s.size();\n unionFind uf(n);\n for (auto& pair : pairs) {\n uf.unionf(pair[0], pair[1]);\n }\n\n string resStr(s);\n vector<vector<int>> res = uf.connectedComponents(n);\n for (auto& v : res) {\n sort(v.begin(), v.end());\n vector<char> tmp;\n for (int i = 0; i < v.size(); i++) {\n tmp.push_back(s[v[i]]);\n }\n sort(tmp.begin(), tmp.end());\n for (int i = 0; i < v.size(); i++) {\n resStr[v[i]] = tmp[i];\n }\n }\n return resStr;\n }\n};\n", "memory": "55812" }
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbsp;<strong>any number of times</strong>.</p> <p>Return the&nbsp;lexicographically smallest string that <code>s</code>&nbsp;can be changed to after using the swaps.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2]] <strong>Output:</strong> &quot;bacd&quot; <strong>Explaination:</strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[1] and s[2], s = &quot;bacd&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2],[0,2]] <strong>Output:</strong> &quot;abcd&quot; <strong>Explaination: </strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[0] and s[2], s = &quot;acbd&quot; Swap s[1] and s[2], s = &quot;abcd&quot;</pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;cba&quot;, pairs = [[0,1],[1,2]] <strong>Output:</strong> &quot;abc&quot; <strong>Explaination: </strong> Swap s[0] and s[1], s = &quot;bca&quot; Swap s[1] and s[2], s = &quot;bac&quot; Swap s[0] and s[1], s = &quot;abc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs[i][0], pairs[i][1] &lt;&nbsp;s.length</code></li> <li><code>s</code>&nbsp;only contains lower case English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n class UnionFind {\n vector<int> ids, sz;\n int num_components;\n\n public:\n UnionFind(int n) : ids(n), sz(n, 1), num_components(n) {\n for (int i = 0; i < n; ++i) {\n ids[i] = i;\n }\n }\n\n int find(int p) {\n if (p == ids[p]) return p;\n return find(ids[p]);\n }\n\n void merge(int p, int q) {\n p = find(p), q = find(q);\n if (p == q) return;\n if (sz[p] < sz[q]) {\n sz[q] += sz[p];\n ids[p] = q;\n } else {\n sz[p] += sz[q];\n ids[q] = p;\n }\n --num_components;\n }\n\n int get_components() const {\n return num_components;\n }\n };\n\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n const int n = s.size();\n UnionFind uf(n);\n for (const auto& p : pairs) {\n uf.merge(p[0], p[1]);\n }\n unordered_map<int, pair<vector<int>, vector<char>>> groups;\n for (int i = 0; i < n; ++i) {\n groups[uf.find(i)].first.push_back(i);\n groups[uf.find(i)].second.push_back(s[i]);\n }\n string smallest = s;\n for (auto itr : groups) {\n auto& indexes = itr.second.first;\n auto& chars = itr.second.second;\n sort(indexes.begin(), indexes.end());\n sort(chars.begin(), chars.end());\n for (int i = 0; i < indexes.size(); ++i) {\n smallest[indexes[i]] = chars[i];\n }\n }\n return smallest;\n }\n};\n\n/*\n\ndcab\n\n0,3\n1,2\n\nbd ac\nbacd\n\ncollecting the pairs in a group\n dfs\n visited indexes\nsort the indexes in the group\n\n*/", "memory": "56337" }
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbsp;<strong>any number of times</strong>.</p> <p>Return the&nbsp;lexicographically smallest string that <code>s</code>&nbsp;can be changed to after using the swaps.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2]] <strong>Output:</strong> &quot;bacd&quot; <strong>Explaination:</strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[1] and s[2], s = &quot;bacd&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2],[0,2]] <strong>Output:</strong> &quot;abcd&quot; <strong>Explaination: </strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[0] and s[2], s = &quot;acbd&quot; Swap s[1] and s[2], s = &quot;abcd&quot;</pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;cba&quot;, pairs = [[0,1],[1,2]] <strong>Output:</strong> &quot;abc&quot; <strong>Explaination: </strong> Swap s[0] and s[1], s = &quot;bca&quot; Swap s[1] and s[2], s = &quot;bac&quot; Swap s[0] and s[1], s = &quot;abc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs[i][0], pairs[i][1] &lt;&nbsp;s.length</code></li> <li><code>s</code>&nbsp;only contains lower case English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int find(int x,vector<int> &parent){\n if(parent[x]==-1){\n return x;\n }\n return parent[x]=find(parent[x],parent);\n }\n\n void uni(int u,int v,vector<int> &rank,vector<int> &parent){\n int p1=find(u,parent);\n int p2=find(v,parent);\n\n if(p1!=p2){\n if(rank[p1]>rank[p2]){\n parent[p2]=p1;\n }\n else if(rank[p1]<rank[p2]){\n parent[p1]=p2;\n }\n else{\n parent[p2]=p1;\n rank[p1]++;\n }\n }\n }\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int n=s.size();\n\n vector<int> parent(n,-1);\n vector<int> rank(n,1);\n\n for(int i=0;i<pairs.size();i++){\n int u=pairs[i][0];\n int v=pairs[i][1];\n\n uni(u,v,rank,parent);\n }\n\n vector<pair<char,int>> v;\n for(int i=0;i<n;i++){\n v.push_back({s[i],i});\n }\n\n vector<vector<int>> comp(n);\n\n sort(v.begin(),v.end());\n\n for(int i=0;i<n;i++){\n int p=find(v[i].second,parent);\n comp[p].push_back(v[i].second);\n }\n\n vector<int> pos(n);\n\n string ans(n,'0');\n\n for(int i=0;i<n;i++){\n int p=find(i,parent);\n int ind=comp[p][pos[p]];\n ans[i]=s[ind];\n pos[p]++;\n }\n\n // for(int i=0;i<n;i++){\n // cout<<i<<\"->\";\n // for(int j=0;j<comp[i].size();j++){\n // cout<<comp[i][j]<<\" \";\n // }\n // cout<<endl;\n // }\n\n return ans;\n }\n};", "memory": "56337" }
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbsp;<strong>any number of times</strong>.</p> <p>Return the&nbsp;lexicographically smallest string that <code>s</code>&nbsp;can be changed to after using the swaps.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2]] <strong>Output:</strong> &quot;bacd&quot; <strong>Explaination:</strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[1] and s[2], s = &quot;bacd&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2],[0,2]] <strong>Output:</strong> &quot;abcd&quot; <strong>Explaination: </strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[0] and s[2], s = &quot;acbd&quot; Swap s[1] and s[2], s = &quot;abcd&quot;</pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;cba&quot;, pairs = [[0,1],[1,2]] <strong>Output:</strong> &quot;abc&quot; <strong>Explaination: </strong> Swap s[0] and s[1], s = &quot;bca&quot; Swap s[1] and s[2], s = &quot;bac&quot; Swap s[0] and s[1], s = &quot;abc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs[i][0], pairs[i][1] &lt;&nbsp;s.length</code></li> <li><code>s</code>&nbsp;only contains lower case English letters.</li> </ul>
1
{ "code": "using ll = long long;\nclass DSU{\n public:\n vector<int> rank;\n vector<int> parent;\n int n;\n DSU(int n){\n this->n=n;\n rank=vector<int>(n,1);\n parent=vector<int>(n,0);\n iota(parent.begin(),parent.end(),0);\n \n }\n ll find(ll a){\n if(parent[a]==a) return a;\n else return parent[a]=find(parent[a]);\n }\n void unionF(ll a , ll b ){\n ll pa = find(a);\n ll pb = find(b);\n if(pa==pb) return ;\n if(rank[pa]>rank[pb]){\n swap(pa,pb);\n }\n parent[pa]=pb;\n if (rank[pa] == rank[pb])\n rank[pb]++;\n }\n \n\n};\nclass Solution {\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& p) {\n DSU ds(s.size());\n for(auto i:p){\n ds.unionF(i[0],i[1]);\n }\n // priority_queue<char, vector<char>, greater<char> >\n map<int,priority_queue<char, vector<char>, greater<char>>> pq;\n for(int i=0;i<s.size();i++){\n pq[ds.find(i)].push(s[i]);\n // cout<<ds.find(i)<<\"->\"<<s[i]<<\" \";\n }\n // cout<<endl;\n\n\n for(int i=0;i<s.size();i++){\n int par=ds.find(i);\n s[i]=pq[par].top();\n // cout<<pq[par].top()<<\"->\"<<par<<\" \"<<i<<\" \";\n pq[par].pop();\n }\n return s;\n\n \n }\n};", "memory": "56862" }
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbsp;<strong>any number of times</strong>.</p> <p>Return the&nbsp;lexicographically smallest string that <code>s</code>&nbsp;can be changed to after using the swaps.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2]] <strong>Output:</strong> &quot;bacd&quot; <strong>Explaination:</strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[1] and s[2], s = &quot;bacd&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2],[0,2]] <strong>Output:</strong> &quot;abcd&quot; <strong>Explaination: </strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[0] and s[2], s = &quot;acbd&quot; Swap s[1] and s[2], s = &quot;abcd&quot;</pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;cba&quot;, pairs = [[0,1],[1,2]] <strong>Output:</strong> &quot;abc&quot; <strong>Explaination: </strong> Swap s[0] and s[1], s = &quot;bca&quot; Swap s[1] and s[2], s = &quot;bac&quot; Swap s[0] and s[1], s = &quot;abc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs[i][0], pairs[i][1] &lt;&nbsp;s.length</code></li> <li><code>s</code>&nbsp;only contains lower case English letters.</li> </ul>
1
{ "code": "using ll = long long;\nclass DSU{\n public:\n vector<int> rank;\n vector<int> parent;\n int n;\n DSU(int n){\n this->n=n;\n rank=vector<int>(n,1);\n parent=vector<int>(n,0);\n iota(parent.begin(),parent.end(),0);\n \n }\n ll find(ll a){\n if(parent[a]==a) return a;\n else return parent[a]=find(parent[a]);\n }\n void unionF(ll a , ll b ){\n ll pa = find(a);\n ll pb = find(b);\n if(pa==pb) return ;\n if(rank[pa]>rank[pb]){\n swap(pa,pb);\n }\n parent[pa]=pb;\n if (rank[pa] == rank[pb])\n rank[pb]++;\n }\n \n\n};\nclass Solution {\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& p) {\n DSU ds(s.size());\n for(auto i:p){\n ds.unionF(i[0],i[1]);\n }\n // priority_queue<char, vector<char>, greater<char> >\n map<int,priority_queue<char, vector<char>, greater<char>>> pq;\n for(int i=0;i<s.size();i++){\n pq[ds.find(i)].push(s[i]);\n // cout<<ds.find(i)<<\"->\"<<s[i]<<\" \";\n }\n // cout<<endl;\n\n\n for(int i=0;i<s.size();i++){\n int par=ds.find(i);\n s[i]=pq[par].top();\n // cout<<pq[par].top()<<\"->\"<<par<<\" \"<<i<<\" \";\n pq[par].pop();\n }\n return s;\n\n \n }\n};", "memory": "56862" }
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbsp;<strong>any number of times</strong>.</p> <p>Return the&nbsp;lexicographically smallest string that <code>s</code>&nbsp;can be changed to after using the swaps.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2]] <strong>Output:</strong> &quot;bacd&quot; <strong>Explaination:</strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[1] and s[2], s = &quot;bacd&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2],[0,2]] <strong>Output:</strong> &quot;abcd&quot; <strong>Explaination: </strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[0] and s[2], s = &quot;acbd&quot; Swap s[1] and s[2], s = &quot;abcd&quot;</pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;cba&quot;, pairs = [[0,1],[1,2]] <strong>Output:</strong> &quot;abc&quot; <strong>Explaination: </strong> Swap s[0] and s[1], s = &quot;bca&quot; Swap s[1] and s[2], s = &quot;bac&quot; Swap s[0] and s[1], s = &quot;abc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs[i][0], pairs[i][1] &lt;&nbsp;s.length</code></li> <li><code>s</code>&nbsp;only contains lower case English letters.</li> </ul>
1
{ "code": "class Solution {\n private:\n vector<int> parent;\n vector<int> rank;\n\n public:\n int find(int x) {\n if (parent[x] == x) return x;\n return parent[x] = find(parent[x]);\n }\n void join(int x, int y) {\n int p1 = find(x);\n int p2 = find(y);\n if (p1 != p2) {\n if (rank[p1] > rank[p2]) {\n parent[p2] = p1;\n } else if (rank[p1] < rank[p2]) {\n parent[p1] = p2;\n } else {\n parent[p2] = p1;\n rank[p2]++;\n }\n }\n }\n string smallestStringWithSwaps(string s, vector<vector<int>> &pairs) {\n int n = s.length();\n parent.resize(n);\n rank.resize(n);\n for (int i = 0; i < n; ++i) {\n parent[i] = i;\n rank[i] = 1;\n }\n\n for (auto pair : pairs) {\n int a = pair[0];\n int b = pair[1];\n join(a, b);\n }\n\n unordered_map<int, vector<int>> group_index;\n for (int i = 0; i < n; ++i) {\n group_index[find(i)].push_back(i);\n }\n\n for (const auto &[key, indices] : group_index) {\n string sub_string;\n for (auto index : indices) {\n sub_string.push_back(s[index]);\n }\n sort(sub_string.begin(), sub_string.end());\n for (int i = 0; i < indices.size(); ++i) {\n s[indices[i]] = sub_string[i];\n }\n }\n\n return s;\n }\n};", "memory": "57387" }
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbsp;<strong>any number of times</strong>.</p> <p>Return the&nbsp;lexicographically smallest string that <code>s</code>&nbsp;can be changed to after using the swaps.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2]] <strong>Output:</strong> &quot;bacd&quot; <strong>Explaination:</strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[1] and s[2], s = &quot;bacd&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2],[0,2]] <strong>Output:</strong> &quot;abcd&quot; <strong>Explaination: </strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[0] and s[2], s = &quot;acbd&quot; Swap s[1] and s[2], s = &quot;abcd&quot;</pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;cba&quot;, pairs = [[0,1],[1,2]] <strong>Output:</strong> &quot;abc&quot; <strong>Explaination: </strong> Swap s[0] and s[1], s = &quot;bca&quot; Swap s[1] and s[2], s = &quot;bac&quot; Swap s[0] and s[1], s = &quot;abc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs[i][0], pairs[i][1] &lt;&nbsp;s.length</code></li> <li><code>s</code>&nbsp;only contains lower case English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n vector<int> parents;\n vector<int> ranks;\n int uffind(int a){\n if(parents[a] == a)return a;\n parents[a] = uffind(parents[a]);\n return parents[a];\n }\n void ufjoin(int a, int b){\n int aroot{uffind(a)}, broot{uffind(b)};\n if(aroot == broot)return;\n if(ranks[aroot] > ranks[broot]){\n ranks[aroot] += ranks[broot];\n parents[broot] = aroot; \n }\n else{\n ranks[broot] += ranks[aroot];\n parents[aroot] = broot; \n }\n }\n\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n const int S{(int)s.size()};\n parents.resize(S);\n ranks.resize(S, 1);\n iota(parents.begin(), parents.end(),0);\n for(vector<int>& p : pairs) ufjoin(p[0], p[1]);\n vector<vector<int>> d(S);\n for(int i = 0; i < S; i++) d[uffind(i)].push_back(i);\n vector<vector<int>> dcpy{d};\n for(int i = 0; i < S; i++){\n sort(dcpy[i].begin(), dcpy[i].end(),[&s](int a, int b){return s[a] < s[b];});\n }\n string res{s};\n for(int i = 0; i< S; i++){\n for(int j = 0; j < d[i].size(); j++){\n res[d[i][j]] = s[dcpy[i][j]];\n }\n }\n return res;\n\n }\n};", "memory": "57387" }
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbsp;<strong>any number of times</strong>.</p> <p>Return the&nbsp;lexicographically smallest string that <code>s</code>&nbsp;can be changed to after using the swaps.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2]] <strong>Output:</strong> &quot;bacd&quot; <strong>Explaination:</strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[1] and s[2], s = &quot;bacd&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2],[0,2]] <strong>Output:</strong> &quot;abcd&quot; <strong>Explaination: </strong> Swap s[0] and s[3], s = &quot;bcad&quot; Swap s[0] and s[2], s = &quot;acbd&quot; Swap s[1] and s[2], s = &quot;abcd&quot;</pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;cba&quot;, pairs = [[0,1],[1,2]] <strong>Output:</strong> &quot;abc&quot; <strong>Explaination: </strong> Swap s[0] and s[1], s = &quot;bca&quot; Swap s[1] and s[2], s = &quot;bac&quot; Swap s[0] and s[1], s = &quot;abc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs.length &lt;= 10^5</code></li> <li><code>0 &lt;= pairs[i][0], pairs[i][1] &lt;&nbsp;s.length</code></li> <li><code>s</code>&nbsp;only contains lower case English letters.</li> </ul>
1
{ "code": "#include<bits/stdc++.h>\n#define ll long long\nusing namespace std;\n\nclass disjoint {\n ll* parent;\n ll* size;\npublic:\n disjoint(ll n) {\n parent = new ll[n];\n size = new ll[n];\n for (ll i = 0; i < n; i++) {\n parent[i] = i;\n size[i] = 1;\n }\n }\n\n ~disjoint() {\n delete[] parent;\n delete[] size;\n }\n\n ll findPar(ll i) {\n if (parent[i] == i) {\n return i;\n }\n return parent[i] = findPar(parent[i]);\n }\n\n void unionBySize(ll x, ll y) {\n ll ulpx = findPar(x);\n ll ulpy = findPar(y);\n if(ulpx!=ulpy){\n if (size[ulpx] > size[ulpy]) {\n parent[ulpy] = ulpx;\n size[ulpx] += size[ulpy];\n }\n else {\n parent[ulpx] = ulpy;\n size[ulpy] += size[ulpx];\n }\n }\n }\n};\nclass Solution {\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n ll n=s.size();\n disjoint ds(n);\n for(auto it:pairs){\n ds.unionBySize(it[0],it[1]);\n }\n map<ll,vector<char>> mpp;\n for(ll i=0;i<n;i++){\n ll p=ds.findPar(i);\n mpp[p].push_back(s[i]);\n }\n for(auto& it:mpp){\n sort(it.second.begin(),it.second.end());\n \n }\n string ans=\"\";\n for(ll i=0;i<n;i++){\n ll p=ds.findPar(i);\n ans+=mpp[p].front();\n mpp[p].erase(mpp[p].begin());\n }\n return ans;\n }\n};", "memory": "57912" }