惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
Vercel News
Vercel News
Microsoft Azure Blog
Microsoft Azure Blog
爱范儿
爱范儿
N
Netflix TechBlog - Medium
Google DeepMind News
Google DeepMind News
H
Help Net Security
罗磊的独立博客
The Cloudflare Blog
J
Java Code Geeks
博客园 - 叶小钗
I
InfoQ
B
Blog
Blog — PlanetScale
Blog — PlanetScale
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
腾讯CDC
月光博客
月光博客
博客园_首页
雷峰网
雷峰网
M
MIT News - Artificial intelligence
博客园 - 【当耐特】
美团技术团队
T
The Blog of Author Tim Ferriss
博客园 - 司徒正美

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Linked List Operations in JavaScript: A Complete Step-by-...
Abhishek Gup · 2026-04-27 · via DEV Community

Abhishek Gupta

🧱 1. What is a Linked List?

A Linked List is a linear data structure where elements are stored in nodes, and each node points to the next.

[value | next] → [value | next] → [value | null]

Enter fullscreen mode Exit fullscreen mode

👉 Last node always points to null


🔧 2. Node Structure

function Node(value) {
  this.data = value;
  this.next = null;
}

Enter fullscreen mode Exit fullscreen mode


🏗️ 3. Linked List Class

var MyLinkedList = function () {
    this.head = null;
    this.len = 0; // MUST be 0
};

Enter fullscreen mode Exit fullscreen mode


⚠️ IMPORTANT RULES (DO NOT IGNORE)

  • Always use 0-based indexing
  • Always update len
  • Never break pointer chain
  • Always check invalid index

📌 4. Add At Head

🖼️ Visualization

💡 Idea

New node becomes the first node.


🪜 Steps

  1. Create new node
  2. Point it to current head
  3. Move head to new node

✅ Code

MyLinkedList.prototype.addAtHead = function (val) {
    const node = new Node(val);

    node.next = this.head;
    this.head = node;

    this.len++;
};

Enter fullscreen mode Exit fullscreen mode


⏱ Complexity

  • O(1)

📌 5. Add At Tail

🖼️ Visualization


💡 Idea

Go to last node → attach new node


🪜 Steps

  1. If empty → head = node
  2. Else → traverse to last
  3. Connect last.next = node

✅ Code

MyLinkedList.prototype.addAtTail = function (val) {
    const node = new Node(val);

    if (!this.head) {
        this.head = node;
        this.len++;
        return;
    }

    let current = this.head;

    while (current.next) {
        current = current.next;
    }

    current.next = node;
    this.len++;
};

Enter fullscreen mode Exit fullscreen mode


⏱ Complexity

  • O(n)

📌 6. Get Value at Index

🖼️ Visualization


💡 Idea

Traverse from head to index


🪜 Steps

  1. Check bounds
  2. Move step-by-step
  3. Return value

✅ Code

MyLinkedList.prototype.get = function (index) {
    if (index < 0 || index >= this.len) return -1;

    let current = this.head;

    for (let i = 0; i < index; i++) {
        current = current.next;
    }

    return current.data;
};

Enter fullscreen mode Exit fullscreen mode


⏱ Complexity

  • O(n)

📌 7. Add At Index

🖼️ Visualization


💡 Idea

Reach index - 1, then insert


🪜 Steps

  1. If index invalid → return
  2. If index = 0 → head
  3. If index = len → tail
  4. Else:
  • go to index - 1
  • insert node

✅ Code

MyLinkedList.prototype.addAtIndex = function (index, val) {
    if (index < 0 || index > this.len) return;

    if (index === 0) {
        this.addAtHead(val);
        return;
    }

    if (index === this.len) {
        this.addAtTail(val);
        return;
    }

    let prev = this.head;

    for (let i = 0; i < index - 1; i++) {
        prev = prev.next;
    }

    const node = new Node(val);
    node.next = prev.next;
    prev.next = node;

    this.len++;
};

Enter fullscreen mode Exit fullscreen mode


⏱ Complexity

  • O(n)

📌 8. Delete At Index

🖼️ Visualization

💡 Idea

Skip the node (bypass)


🪜 Steps

  1. Validate index
  2. If index = 0 → move head
  3. Else:
  • go to index - 1
  • bypass node

✅ Code

MyLinkedList.prototype.deleteAtIndex = function (index) {
    if (index < 0 || index >= this.len) return;

    if (index === 0) {
        this.head = this.head.next;
        this.len--;
        return;
    }

    let prev = this.head;

    for (let i = 0; i < index - 1; i++) {
        prev = prev.next;
    }

    let toDelete = prev.next;
    prev.next = toDelete.next;

    toDelete.next = null; // cleanup

    this.len--;
};

Enter fullscreen mode Exit fullscreen mode


⏱ Complexity

  • O(n)

🔥 FULL WORKING EXAMPLE

var obj = new MyLinkedList();

obj.addAtHead(10);
obj.addAtHead(20);
obj.addAtTail(30);
obj.addAtIndex(1, 99);

console.log(obj.get(1)); // 99

obj.deleteAtIndex(1);

console.log(obj.get(1)); // 10

Enter fullscreen mode Exit fullscreen mode


🧠 MONSTER LEVEL UNDERSTANDING


🔑 Core Pattern

👉 Always work with previous node

prev.next = prev.next.next;

Enter fullscreen mode Exit fullscreen mode


🔑 Golden Rule

Linked List = Pointer manipulation, not value shifting


🔑 Why Array vs Linked List

Feature Array Linked List
Insert O(n) O(1) (if pointer known)
Delete O(n) O(1)
Access O(1) O(n)

🔑 Most Important Interview Insight

You NEVER delete node directly
You only change links