Linked List & List Node All In One

链表 & 节点

链表类型

  1. 单链表
  2. 双链表
  3. 环形链表 / 循环链表

Singly Linked List (Uni-directional)

Doubly Linked List (Bi-directional)

Circular Linked List

js 实现 Linked List


"use strict"; /**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-11-17
* @modified
*
* @description 链表 & 节点
* @difficulty Easy Medium Hard
* @complexity O(n)
* @augments
* @example
* @link
* @solutions
*
* @best_solutions
*
*/ const log = console.log; // 节点
function ListNode(val, next) {
this.val = 0 || val;
this.next = null || next;
} // 链表
function LinkedList(value) {
const node = new ListNode(value, ``);
if(!head) {
head = node;
} else {
let current = head;
while(current.next) {
current = current.next;
}
current.next = node;
}
};
function LinkedList () {
// init & 闭包 closure
let length = 0;
let head = null;
// methods
this.append = function(value) {
const node = new ListNode(value, ``);
if(!head) {
head = node;
} else {
let current = head;
while(current.next) {
current = current.next;
}
current.next = node;
}
// log(`head =\n`, head)
length += 1;
}
this.getList = function() {
// log(`head =`, head)
return head;
}
} const test = new LinkedList(); test.append(1);
test.append(2);
// test.append(3); reverseList(test.getList()) /* head = ListNode { val: 1, next: ListNode { val: 2, next: ListNode { val: 3, next: '' } } }
*/

反转链表

https://leetcode.com/problems/reverse-linked-list/


/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var reverseList = function(head) {
let [prev, curr] = [null, head];
while (curr) {
let tmp = curr.next; // 1. 临时存储当前指针后续内容
curr.next = prev; // 2. 反转链表
prev = curr; // 3. 接收反转结果
curr = tmp; // 4. 接回临时存储的后续内容
}
return prev;
};

var reverseList = function(head) {
let [prev, curr] = [null, head];
while (curr) {
// swap
[curr.next, prev, curr] = [prev, curr, curr.next];
}
return prev;
};

refs

https://www.educative.io/edpresso/what-is-a-linked-list

https://people.engr.ncsu.edu/efg/210/s99/Notes/LinkedList.1.html

PPT

https://www.slideshare.net/sshinchan/single-linked-list



xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


Linked List & List Node All In One的更多相关文章

  1. [LeetCode] Linked List Random Node 链表随机节点

    Given a singly linked list, return a random node's value from the linked list. Each node must have t ...

  2. Leetcode 382. Linked List Random Node

    本题可以用reservoir sampling来解决不明list长度的情况下平均概率选择元素的问题. 假设在[x_1,...,x_n]只选一个元素,要求每个元素被选中的概率都是1/n,但是n未知. 其 ...

  3. 382. Linked List Random Node

    Given a singly linked list, return a random node's value from the linked list. Each node must have t ...

  4. [Swift]LeetCode382. 链表随机节点 | Linked List Random Node

    Given a singly linked list, return a random node's value from the linked list. Each node must have t ...

  5. Reservoir Sampling-382. Linked List Random Node

    Given a singly linked list, return a random node's value from the linked list. Each node must have t ...

  6. [LeetCode] 382. Linked List Random Node ☆☆☆

    Given a singly linked list, return a random node's value from the linked list. Each node must have t ...

  7. Linked List Random Node -- LeetCode

    Given a singly linked list, return a random node's value from the linked list. Each node must have t ...

  8. [LeetCode] 382. Linked List Random Node 链表随机节点

    Given a singly linked list, return a random node's value from the linked list. Each node must have t ...

  9. 【LeetCode】382. Linked List Random Node 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数组保存再随机选择 蓄水池抽样 日期 题目地址:ht ...

随机推荐

  1. CTO也糊涂的常用术语:功能模块、业务架构、用户需求、文档……

    功能模块.业务架构.需求分析.用户需求.系统分析.功能设计.详细设计.文档.业务.技术--很多被随口使用的名词,其实是含糊甚至错误的. 到底含糊在哪里,错误在哪里,不仅仅是新手软件开发人员糊涂,许多入 ...

  2. SuperUpdate.sh 一键更换Linux软件源脚本

    一.前言 有时候会遇到 Linux 的源更新速度非常的缓慢,特别是在国内使用默认的源,因为国内的网络环境,经常会出现无法更新,更新缓慢的情况.在这种情况下,更换一个更适合或者说更近,更快的软件源,会为 ...

  3. 如何实现new,call,apply,bind的底层原理。

    new做了什么? new是用来实例化对象的,当new了一个对象后 1.创建一个新对象 2.将构造函数的作用域赋值给新对象(this指向新对象) 3.执行构造函数里面的代码(为这个新对象添加属性) 4. ...

  4. Bitter.Core系列五:Bitter ORM NETCORE ORM 全网最粗暴简单易用高性能的 NETCore ORM 之 示例 分页聚联查询

    Bitter.Core 在聚联/分页聚联查询的时候,采用原生的MSSQL, MYSQL 语句查询,做过复杂高级项目的人知道,原生的聚合查询代码执行效率更高,更快,更容易书写,开发量最少. 借助原生的M ...

  5. 向HDFS中上传任意文本文件,如果指定的文件在HDFS中已经存在,由用户指定是追加到原有文件末尾还是覆盖原有的文件

    1 import java.io.FileInputStream; 2 import java.io.IOException; 3 import java.util.Scanner; 4 5 impo ...

  6. BIO,NIO,AIO 总结

    BIO,NIO,AIO 总结 Java 中的 BIO.NIO和 AIO 理解为是 Java 语言对操作系统的各种 IO 模型的封装.程序员在使用这些 API 的时候,不需要关心操作系统层面的知识,也不 ...

  7. pycharm创建文件夹以及查看源文件存放位置(FOR MAC)

    1.首先我们打开pycharm软件,一般首次打开界面如下 2.我们点击creat new project,点击后效果如下,此时创建相应的文件夹 3.创建完成后如下,并可以查看存放文件夹的位置 4.完成 ...

  8. MarkDown学习笔记 Typora

    快捷方式篇 新建 ctrl + N 新建窗口 ctrl + shift + N 打开md文件 ctrl + O 快速打开 ctrl + P 保存 ctrl + S 另存为 ctrl + shift + ...

  9. java解压zip压缩包

    package com.spring.mvc.zip; import java.io.File;import java.io.FileOutputStream;import java.io.Input ...

  10. SpringMVC系列(一)核心:处理请求流程

    http://blog.csdn.net/zhaolijing2012/article/details/41596803