头图

Zero title: Algorithm (leetcode, with mind map + all solutions) (160) intersecting linked list of 300 questions

a topic description

题目描述
题目描述
题目描述

Two solutions overview (mind map)

思维导图

All three solutions

1 Scenario 1

1) Code:

 // 方案1 “自己。哈希法(JS里的Map数据结构)”。

// 思路:
// 1)状态初始化:resMap = new Map(), resNode = null; 。
// 2)核心1:遍历 链表A ,将每个节点存入 哈希resMap 中。
// 3)核心2:遍历 链表B 。
// 3.1)若 当前节点是否存在于 哈希resMap 中,则 resNode 置为当前节点 并 退出遍历。
// 4)返回结果 resNode 。
var getIntersectionNode = function(headA, headB) {
    // 1)状态初始化:resMap = new Map(), resNode = null; 。
    let resMap = new Map(),
        resNode = null;

    // 2)核心1:遍历 链表A ,将每个节点存入 哈希resMap 中。
    while (headA) {
        resMap.set(headA, 1);
        headA = headA.next;
    }

    // 3)核心2:遍历 链表B 。
    while (headB) {
        // 3.1)若 当前节点是否存在于 哈希resMap 中,则 resNode 置为当前节点 并 退出遍历。
        if (resMap.has(headB)) {
            resNode = headB;
            break;
        }
        headB = headB.next;
    }

    // 4)返回结果 resNode 。
    return resNode;
}

2 Option 2

1) Code:

 // 方案2 “双指针法。”。
// 参考:
// 1)https://leetcode.cn/problems/intersection-of-two-linked-lists/solution/xiang-jiao-lian-biao-by-leetcode-solutio-a8jn/

// 思路:
// 1)每步操作需要同时更新指针 pA 和 pB。
// 2)若 指针 pA 不为空,则 将指针 pA 移到下一个节点;若 指针 pB 不为空,则 将指针 pB 移到下一个节点。
// 3)若 指针 pA 为空,则 将指针 pA 移到链表 headB 的头节点;若 指针 pB 为空,则 将指针 pB 移到链表 headA 的头节点。
// 4)若 当指针 pA 和 pB 指向同一个节点 或 都为空时,则 返回它们指向的节点 或 null 。
var getIntersectionNode = function(headA, headB) {
    let pA = headA, pB = headB;

    while (pA !== pB) {
        pA = pA === null ? headB : pA.next;
        pB = pB === null ? headA : pB.next;
    }

    return pA;
}

Four resource sharing & more

1 Historical Articles - Overview

Article name solution reading volume
1. Two Sum 3 types in total 2.7k+
2. Add Two Numbers 4 types in total 2.7k+
3. Longest Substring Without Repeating Characters 3 types in total 2.6k+
4. Find the Median of Two Sorted Arrays 3 types in total 2.8k+
5. Longest Palindromic Substring 4 types in total 2.8k+
6. ZigZag Conversion 2 types in total 1.9k+
7. Reverse Integer 2 types in total 2.4k+
8. String to Integer (atoi) 3 types in total 4.2k+
9. Palindrome Number 3 types in total 4.3k+
11. Container With Most Water 5 in total 4.0k+
12. Integer to Roman 3 types in total 3.2k+
13. Roman to Integer 3 types in total 3.8k+
14. Longest Common Prefix 4 types in total 3.0k+
15. The Sum of Three Numbers (3Sum) 3 types in total 60.7k+
16. 3Sum Closest 3 types in total 4.7k+
17. Letter Combinations of a Phone Number 3 types in total 3.1k+
18. The sum of four numbers (4Sum) 4 types in total 11.5k+
19. Remove Nth Node From End of List 4 types in total 1.2k+
20. Valid Parentheses 2 types in total 1.8k+
21. Merge Two Sorted Lists 3 types in total 1.2k+
22. Generate Parentheses 4 types in total 1.1k+
23. Merge k Sorted Lists 4 types in total 0.9k+
24. Swap Nodes in Pairs 3 types in total 0.5k+
25. Reverse Nodes in k-Group 5 in total 1.3k+
26. Remove Duplicates from Sorted Array 4 types in total 1.3k+
27. Remove Element 4 types in total 0.4k+
28. Implement strStr() (Implement strStr()) 5 in total 0.8k+
29. Divide Two Integers 4 types in total 0.6k+
30. Substring with Concatenation of All Words 3 types in total 0.6k+
31. Next Permutation 2 types in total 0.8k+
32. Longest Valid Parentheses 2 types in total 1.4k+
33. Search in Rotated Sorted Array 3 types in total 1.0k+
34. Find First and Last Position of Element in Sorted Array 3 types in total 0.5k+
35. Search Insert Position 3 types in total 0.3k+
36. Valid Sudoku 1 in total 0.6k+
38. Count and Say 5 in total 1.1k+
39. Combination Sum 3 types in total 1.4k+
40. Combination Sum II 2 types in total 1.6k+
41. First Missing Positive 3 types in total 1.2k+
53. Maximum Subarray Sum (Maximum Subarray) 3 types in total 0.3k+
88. Merge Sorted Array 3 types in total 0.4k+
102. Binary Tree Level Order Traversal 3 types in total 0.4k+
146. LRU Cache (LRU Cache) 2 types in total 0.5k+
206. Reverse Linked List 3 types in total 1.0k+
215. Kth Largest Element in an Array 3 types in total 0.5k+
236. Lowest Common Ancestor of a Binary Tree 3 types in total 0.1k+

刷题进度 - LeetCode:527 / 2662 、《剑指offer》:66 / 66

2 Introduction to bloggers

Code Farmer Sanshao, a blogger dedicated to writing minimalist but complete problem solutions (algorithms ).
Focus on one question, multiple solutions, structured thinking , welcome to brush through LeetCode ~


码农三少
54 声望8 粉丝