0
点赞
收藏
分享

微信扫一扫

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

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

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

基本内容 - 数组

数组是内存中连续的一段区域。

数组复杂度 :

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

基本内容 - 链表

适合插入、删除操作多的场景。插入删除调整next 指针。

链表复杂度:

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

题目- 数组排序

各种数组排序算法

题目-反转链表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


题目- 判断链表是否有环


举报

相关推荐

0 条评论