View : 208

12/04/2026 18:16pm

JS2GO EP.32 Using Linked Lists in Go vs JavaScript

JS2GO EP.32 Using Linked Lists in Go vs JavaScript

#programming

#Data Structure

#JavaScript

#Go

#Linked List

A Linked List is a dynamic data structure used to store data where each node is connected (linked) to the next one using a pointer or reference. This structure allows fast insertion and deletion of data compared to arrays in many cases.

 

In this article, we’ll explore how to create and use Linked Lists in JavaScript and Go, with practical code examples and best practices for real-world applications. 🚀

 

1. Concept of Linked Lists

 

A Linked List consists of three main components:

  • Node: Stores the data and a pointer/reference to the next node.
  • Head: The first node in the list.
  • Tail: The last node, whose pointer points to null (in JS) or nil (in Go).

 

Types of Linked Lists:

  • Singly Linked List: Each node points only to the next one.
  • Doubly Linked List: Each node points to both the previous and the next node.

 

2. Linked List in JavaScript

 

JavaScript doesn’t have a built-in Linked List, but you can easily implement one using a class.

 

🔹 Example: Singly Linked List

class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

class LinkedList {
  constructor() {
    this.head = null;
  }

  append(value) {
    const newNode = new Node(value);
    if (!this.head) {
      this.head = newNode;
      return;
    }
    let current = this.head;
    while (current.next) {
      current = current.next;
    }
    current.next = newNode;
  }

  print() {
    let current = this.head;
    while (current) {
      console.log(current.value);
      current = current.next;
    }
  }
}

// Test
const list = new LinkedList();
list.append(10);
list.append(20);
list.append(30);
list.print();

 

Output:

10
20
30

 

Advantages (JavaScript):
✔️ Easy to write and understand using class syntax
✔️ Dynamic structure that resizes automatically
✔️ Ideal for frequent insert/delete operations

 

Limitations:
⚠️ No type checking
⚠️ No built-in Linked List structure in JavaScript

 

3. Linked List in Go

 

Go provides a built-in package container/list for working with Linked Lists directly.

 

🔹 Example: Using container/list

package main

import (
    "container/list"
    "fmt"
)

func main() {
    linkedList := list.New()
    linkedList.PushBack(10)
    linkedList.PushBack(20)
    linkedList.PushBack(30)

    for e := linkedList.Front(); e != nil; e = e.Next() {
        fmt.Println(e.Value)
    }
}

 

Output:

10
20
30

 

🔹 Example: Custom Implementation

package main

import "fmt"

type Node struct {
    Value int
    Next  *Node
}

type LinkedList struct {
    Head *Node
}

func (l *LinkedList) Append(value int) {
    newNode := &Node{Value: value}
    if l.Head == nil {
        l.Head = newNode
        return
    }
    current := l.Head
    for current.Next != nil {
        current = current.Next
    }
    current.Next = newNode
}

func (l *LinkedList) Print() {
    current := l.Head
    for current != nil {
        fmt.Println(current.Value)
        current = current.Next
    }
}

func main() {
    list := LinkedList{}
    list.Append(10)
    list.Append(20)
    list.Append(30)
    list.Print()
}

 

Advantages (Go):
✔️ Type-safe with compile-time checking
✔️ Efficient and memory-optimized
✔️ Suitable for backend and high-performance systems

 

Limitations:
⚠️ Syntax is more verbose than JavaScript
⚠️ Requires manual pointer handling

 

4. Best Practices

 

✔️ Use Linked Lists when frequent insertions or deletions are required — such as in Queues, Stacks, or Scheduling systems.
✔️ Avoid using them for random access, since traversal through each node is required.
✔️ Use container/list in Go if you don’t need to implement from scratch.
✔️ Use class-based structures in JavaScript for clarity and modularity.

 

5. Comparison: JavaScript vs Go

 

FeatureJavaScriptGo
StructureCustom classBuilt-in (container/list)
Type SafetyDynamicStrongly typed
PerformanceModerateHigh efficiency
ImplementationManual (class)Built-in or manual
Best Use CaseLightweight data manipulationHigh-performance systems

 

Recommendations

 

💡 Frontend / App-level logic: Use JavaScript class for flexibility and readability.
⚙️ Backend / High-performance systems: Use Go’s container/list or custom implementation for speed and type safety.

 


 

Next Episode

 

In EP.33 of the JS2GO Series, we’ll explore 👉 “Using Trees and Graphs in Go and JavaScript” understanding advanced data structures used in real-world systems such as social graphs, file systems, and search algorithms. 🌳

 

Read more

🔵 Facebook: Superdev Academy

🔴 YouTube: Superdev Academy

📸 Instagram: Superdev Academy

🎬 TikTok: https://www.tiktok.com/@superdevacademy?lang=th-TH

🌐 Website: https://www.superdevacademy.com/en