数据结构和算法专业-基础

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

2021-05-29  本文已影响0人  捡了幸福的猪

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

基本内容 - 数组

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

数组复杂度 :

基本内容 - 链表

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

链表复杂度:

题目- 数组排序

各种数组排序算法 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


题目- 判断链表是否有环


上一篇 下一篇

猜你喜欢

热点阅读