算法 -- 数组&链表 -- 01

作者: 捡了幸福的猪 | 来源:发表于2021-05-29 21:30 被阅读0次

    前端同学工作需掌握的相关算法内容。
    同系列文章(TODO):

    • 算法复杂度
    • 算法 -- 栈&队列 -- 02
    • 算法 -- 树&二叉树 &二叉搜索树-- 03
    • 算法 -- 二叉树遍历 -- 04
    • 算法 -- 递归&分治 -- 05
      ( 下半年计划...

    基本内容 - 数组

    数组是内存中连续的一段区域。 数组.png 数组的插入与删除png

    数组复杂度 :

    • Access: O(1)
    • Insert: O(n)
    • Delete: O(n)

    基本内容 - 链表

    适合插入、删除操作多的场景。插入删除调整next 指针。 单链表.png 双链表.png

    链表复杂度:

    • Access O(n)
    • Insert O(1)
    • Delete O(1)

    题目- 数组排序

    各种数组排序算法 from https://www.bigocheatsheet.com/

    题目-反转链表No.206

     function ListNode(val, next) {
        this.val = (val===undefined ? 0 : val)
        this.next = (next===undefined ? null : next)
     }
    // 方法1
    const reverseList1 = (head) => {
      if (!head) {
        return null;
      }
      let first = head;
      let second = head.next;
      let third = second;
    
      while(second) {
        third = second.next;
        second.next = first;
        if (first.next === second){
          first.next = null;
        }
        first = second;
        second = third;
      }
      return first;
    };
    
    // 方法2
    const reverseList = (head) => {
      let [prev, current] = [null, head]
    
      while(current) {
          [current.next, prev, current] = [prev, current, current.next]
      }
      return prev
    }
    

    题目-链表两结点反转No.24

    
    

    题目- 判断链表是否有环

    
    

    相关文章

      网友评论

        本文标题:算法 -- 数组&链表 -- 01

        本文链接:https://www.haomeiwen.com/subject/tocusltx.html