xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

Linked List & List Node All In One

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 发布文章使用:只允许注册用户才可以访问!


posted @   xgqfrms  阅读(252)  评论(1编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
历史上的今天:
2019-11-17 CSS3 & transform & skew
2019-11-17 可视化埋点 & 实现思路
2018-11-17 iPad Pro 2018
2015-11-17 RequireJS is a JavaScript file and module loader
2015-11-17 Less.js CSS3
2015-11-17 SASS CSS3 koala
2015-11-17 网页无障碍
点击右上角即可分享
微信分享提示