Fundamentals 6 min read

Master Singly Linked Lists in Go: Build, Insert, Delete, and Traverse

This article introduces the concept, characteristics, and core operations of singly linked lists, then provides a complete Go implementation with node definitions, insertion at head and tail, deletion, traversal, and a test program to demonstrate usage.

Ops Development & AI Practice
Ops Development & AI Practice
Ops Development & AI Practice
Master Singly Linked Lists in Go: Build, Insert, Delete, and Traverse

Singly Linked List Overview

A singly linked list is a dynamic data structure composed of nodes, each holding a data field and a pointer to the next node. Because nodes can reside anywhere in memory, the list provides non‑contiguous storage while preserving order through the pointers.

Characteristics

Dynamic size : Nodes can be inserted or removed at runtime without pre‑allocating a fixed capacity.

Space efficient : Each node stores only one pointer in addition to its payload.

Flexible allocation : Nodes may be allocated independently, avoiding the need to shift elements when the list changes.

Core Operations

Insertion : add a node at the head, tail, or an arbitrary position.

Deletion : remove the head, tail, or a specific node.

Search : traverse the list to locate a node that satisfies a condition.

Traversal : visit each node from head to tail, typically for printing or processing.

Go Implementation of a Singly Linked List

The following Go code defines the node and list types and implements the basic operations.

Node and List Types

package main

import "fmt"

// ListNode defines a list node
type ListNode struct {
    Value int
    Next  *ListNode
}

// LinkedList defines the list structure
type LinkedList struct {
    Head *ListNode
}

Insertion

Insert at head

func (l *LinkedList) InsertAtHead(value int) {
    node := &ListNode{Value: value}
    if l.Head != nil {
        node.Next = l.Head
    }
    l.Head = node
}

Insert at tail

func (l *LinkedList) InsertAtTail(value int) {
    node := &ListNode{Value: value}
    if l.Head == nil {
        l.Head = node
        return
    }
    current := l.Head
    for current.Next != nil {
        current = current.Next
    }
    current.Next = node
}

Deletion

Delete head node

func (l *LinkedList) DeleteHead() {
    if l.Head != nil {
        l.Head = l.Head.Next
    }
}

Traversal

Print all nodes

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

Test Program

package main

import "fmt"

func main() {
    list := LinkedList{}
    list.InsertAtHead(1)
    list.InsertAtTail(2)
    list.InsertAtHead(0)
    list.Print() // Expected output: 0 1 2

    list.DeleteHead()
    list.Print() // Expected output: 1 2
}

Typical time complexities are O(1) for head insertion and deletion, O(n) for tail insertion (due to linear traversal) and for searching. The implementation can be extended with tail pointers, size counters, or generic value types to improve performance and usability.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

programmingGoData StructuresAlgorithmslinked listSingly Linked List
Ops Development & AI Practice
Written by

Ops Development & AI Practice

DevSecOps engineer sharing experiences and insights on AI, Web3, and Claude code development. Aims to help solve technical challenges, improve development efficiency, and grow through community interaction. Feel free to comment and discuss.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.