[Unemployed front-end evil supplement algorithm]JavaScript leetcode top100 (3): intersecting linked list, majority elements, reversed linked list, reversed binary tree, palindrome linked list

Column statement: Just use the simplest and easy-to-understand method to pass, don't seek optimization, don't spray if you don't like it,
update five easy difficulty questions today:

intersecting list

  • Topic
    Given the head nodes headA and headB of two singly linked lists, please find and return the starting node where the two singly linked lists intersect. Returns null if there is no intersecting node between the two linked lists.
    The diagram shows that the two linked lists intersect at node c1:
    the title data ensures that there is no cycle in the entire chain structure.
    Note that the linked list must maintain its original structure after the function returns the result.

  • Knowledge point:
    linked list

  • Idea
    You can understand it this way, if we run the A linked list first, and then the B linked list, and you run the B linked list first, and then the A linked list, the number of nodes traversed will be the same. Then if A and B linked lists have the same part, as long as we traverse A and B, and B and A once each, then the occurrence of pointing to the same node during the traversal indicates that the subsequent parts are consistent. So we traverse A and B in order. If we traverse to the bottom, we will change to another linked list to continue traversing. If there is a situation that points to the same node during the process, we will return this node.

  • the code

var getIntersectionNode = function(headA, headB) {
    
    
    if (headA === null || headB === null) {
    
    
        return null;
    }
    let pA = headA, pB = headB;
    while (pA !== pB) {
    
    
        pA = pA === null ? headB : pA.next;
        pB = pB === null ? headA : pB.next;
    }
    return pA;
};

most elements

  • Topic
    Given an array nums of size n, return the most elements in it. A majority element is an element that occurs more than ⌊ n/2 ⌋ in the array.
    You can assume that arrays are non-empty and that there is always a majority of elements in a given array.

  • Knowledge point
    hash table, divide and conquer

  • Ideas
    We first take the first number of the array as the benchmark, and we traverse the array in turn. If it is the same as the benchmark, the count is +1, otherwise the count is -1. If the count returns to 0, it means that the frequency of this number as our benchmark is not high at present , we use the current number as the benchmark to continue the operation, and the last surviving number is the number we need.
    To explain it in a more vivid way, there are many armies fighting now. Every time you encounter someone who is not your own camp, then a teammate will have to exchange one with the enemy. Now start searching from the camp at the first position of the array. If you encounter the same camp as the current camp, it is a friendly army, and the number of current camps + 1; otherwise, the enemy needs a friendly army to exchange one with the enemy, so the number of current camps - 1 . If the number of camps returns to 0, then the lineup will be destroyed temporarily, and we will start a new traversal from the camp that destroyed them. The last surviving camp is more than half of the camps, because they have at least one extra person after changing one, so they will definitely not be wiped out during the traversal process.

  • the code

var majorityElement = function (nums) {
    
    
    let count = 0;
    let win = -1;
    for (var i = 0; i < nums.length; i++) {
    
    
        if (count == 0) {
    
    
            win = nums[i];
            count++;
        }else if(win == nums[i]){
    
    
            count++;
        }else{
    
    
            count--;
        }
    }
    return win;
};

reverse linked list

  • Topic
    Given the head node head of the singly linked list, please reverse the linked list and return the reversed linked list.
  • Linked list of knowledge points
  • The idea
    head interpolation method forms a linked list, traverses the linked list from front to back, inserts the current node into the head each time, so as the linked list traverses, the first inserted node will come to the end, so the first traversed node of the head Finally came to the end, and the last traversed node at the end of the linked list will come to the head of the linked list, thus completing the reversal of the linked list
  • the code
var reverseList = function (head) {
    
    
    let prev = null;
    let curr = head;
    while (curr) {
    
    
        const next = curr.next;
        curr.next = prev;
        prev = curr;
        curr = next;
    }
    return prev;
};

Flip a binary tree

  • Topic
    Given you the root node root of a binary tree, flip the binary tree and return its root node.
  • Knowledge point
    binary tree, depth-first search
  • Idea
    Depth first traverse the entire binary tree, for each node, swap the left and right children of the binary tree, and then continue to traverse the left and right subtrees of the binary tree
  • the code
var invertTree = function(root) {
    
    
    let dfs = function(node){
    
    
        if(!node){
    
    
            return ;
        }
        t = node.left;
        node.left = node.right;
        node.right = t;
        if(node.right){
    
    
            dfs(node.right)
        }
        if(node.left){
    
    
            dfs(node.left)
        }
    }
    dfs(root);
    return root;
};

palindromic list

  • Topic
    Given you a head node head of a singly linked list, please judge whether the linked list is a palindrome linked list. If yes, return true; otherwise, return false.

  • Linked list of knowledge points
    , palindrome, speed pointer

  • Ideas
    We first find the middle position of the linked list, using two pointers, one fast and one slow, the fast one takes two steps at a time, and the slow one takes one step, so that when the fast pointer arrives, the slow pointer will be aligned with the middle of the linked list . Note that we let both pointers take one step each time. If the fast pointer reaches the bottom, the program stops, and if it does not reach it, let it take another step. After this processing, if it is an odd-numbered linked list, the pointer will stop in the middle, and an even-numbered linked list will stop at the (n/2 + 1)th position.
    After that, we reverse the entire linked list from the position of the slow pointer. The code logic has been mentioned in the above topic. After the reversal is completed, we traverse the original first half of the linked list and the reversed second half of the linked list from the beginning. If the elements of the two are the same, it is a palindrome linked list, otherwise it is not.

  • the code

var isPalindrome = function(head) {
    
    

    let t = head;
    let b = head;
    let a = head;
    while(b){
    
    
        b = b.next;
        a = a.next;
        if(b){
    
    
            b = b.next;
        }
    }
    let re = null;
    while( a ){
    
    
        let t = a.next;
        a.next = re;
        re = a;
        a = t;
    }
    while(re){
    
    
        if(t.val != re.val){
    
    
            return false;
        }
        t = t.next;
        re = re.next;
    }
    return true;
};

Guess you like

Origin blog.csdn.net/weixin_46463785/article/details/129453104