1. Judge palindromes

let num=12345654321 let nums=123456 function palindrome(num){ let arr=(num+'').split("") let num1=arr.reverse().join("") If (num1==num){console.log(" palindrome number "); }else{console.log(" Not palindrome number "); } } palindrome(num) palindrome(nums)Copy the code

In this case, I used the method of string split into array (split), then used the method of array reverse (reverse), and finally used the method of array to string (join), and then determined whether the newly obtained string is equal to the string passed in. In this case, the first call result will be printed on the console — palindrome, The second call prints — not palindromes.

2. Search for the most common characters in a string

let str = "aaassffffddd"; Function duplicatechar (STR) {var duplicatechar = ""; For (var I = 0; i < str.length; i++) { var ci = str[i]; if (! cnt[ci]) { cnt[ci] = 1; } else { cnt[ci]++; } if (c == "" || cnt[ci] > cnt[c]) { c = ci; } } console.log(cnt); return c; }Copy the code

In this case, I loop each character of the string first, in the judgement inside! CNT [ci] is a judgment. If I have a CNT that doesn’t have a character ci in it, I automatically add one to it and if I have a character CI in it, I add one to it, and THEN I say if c is empty, c is the current character. If CNT [ci]> CNT [c] that is, if the occurrence of ci is greater than the occurrence of C, c is reassigned to ci, and c is the most frequently occurring character.

3. Inversion of linked lists

/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var reverseList = function(head) {
    let s=null;
    let n=head;
    // 1 2 3
    while(n){
        // 2 3 null
        let next=n.next
        // 1->null 2->1 3->2
        n.next=s
        // 1 2 3
        s=n
        // 2 3 null
        n=next
    }
     return s

};
Copy the code

{val:1,next:{val:2,next:{val:3,next:null}}} this list is passed through head, passing the value of head to n and then loop n if n exists keep loop if null stops loop.

The first one we pass in is n equals 1 and we declare a next, which is the value of the current next which is 2 and the preceding value of the current value is null and the last value we declare is the current value which is n which is 1 and the current n is equal to the next value which is next which is 2 and we go through the second loop

The second pass is n equals 2 declare a next, Next is going to be the current value next is going to be 3 and the current value is going to be 1(this was copied over in the last loop) and the last declared value is going to be the current value which is going to be n which is going to be 2 and the current n is going to be equal to the next value which is going to be 3 and then we go through the third loop

The third pass is n equals 3 declare a next, Next is the current value next is null and the current value starts with 2(this was copied over in the last loop) and the last declared value is the current value which is n which is 3 and the current n is equal to the next value which is next which is null and so the loop ends

S is now equal to 3 so return s is the list reversed