Singly Linked Lists

Linked Lists: A Detailed Look at Singly Linked Lists

In computer science, the manner in which data is stored and organized can significantly impact performance and efficiency. Among the data structures that developers use to store data, linked lists are often a popular choice due to their inherent versatility and dynamism. In this article, we will explore linked lists, with a special focus on a common variation known as a singly linked list.

What is a Linked List?

In the simplest terms, a linked list is a linear collection of data elements, referred to as nodes, in which the linear order is not given by their physical placement in memory, but by pointers in each node. This abstract data type can be used to implement several other types of data structures, such as queues, stacks, or associative arrays.

Each node in a linked list contains data and a link (or pointer) to the next node in the sequence. This structure allows for efficient insertions and deletions at any position in the sequence, making linked lists a dynamic data structure.

Singly Linked Lists

Singly Linked Lists: A Closer Look

A singly linked list is a basic type of linked list where each node contains data and a reference (or link) to the next node in the sequence. The list is traversed starting from the head (first node) and following the references to the subsequent nodes until the last node, which points to a null reference indicating the end of the list.

Node Structure

In a singly linked list, each node consists of two elements:

  1. Data: This component can hold various types of data, which may include simple data types such as integers, floating-point numbers, or complex data structures like objects, depending on the needs of the application.
  2. Next: This component is a pointer that holds the memory address of the next node in the list. For the last node of the list, the next pointer points to null, indicating the end of the list.

Operations in Singly Linked Lists

There are several common operations that can be performed on a singly linked list:

  1. Insertion: New nodes can be added at any point in the list—at the beginning, in the middle, or at the end. While inserting at the head is a relatively straightforward operation (with constant time complexity O(1)), inserting at the end or in the middle involves traversing the list, which can take up to O(n) time.
  2. Deletion: Nodes can be removed from the list similar to how they’re inserted—at the beginning, middle, or end. The time complexity for deletion also mirrors that of insertion: O(1) at the beginning and up to O(n) in the middle or end.
  3. Search: To find a specific node, you would need to start at the head and traverse the list until you either find the desired node or reach the end of the list. This operation has a worst-case time complexity of O(n).
  4. Traversal: This operation involves visiting every node in the list. Since you have to visit each node once, the time complexity is O(n).

Advantages of Singly Linked Lists

Singly linked lists come with a number of advantages:

  • Dynamic Size: Unlike arrays, singly linked lists don’t have a fixed size. The size can be altered during the execution of a program.
  • Efficient Memory Usage: Memory is allocated as and when needed, which prevents memory waste.
  • Ease of Insertion/Deletion: Nodes can be added or removed with relative ease without needing to rearrange or reallocate the entire data structure.

Disadvantages of Singly Linked Lists

Despite their advantages, singly linked lists also have a few downsides:

  • Sequential Access: Unlike arrays, linked lists do not provide random or direct access to the elements. To access a particular node, you must start from the head and follow the links to the desired node.
  • More Memory: Each node in a singly linked list requires more memory than an array because each node needs to store additional information (the pointer to the next node).
  • Reverse Traversing: In singly linked lists, navigation is only forward. If you need to traverse backward, you either need to maintain another linked list or you must start from the head again.
Singly Linked Lists

Code Examples:

Code implementations of a singly linked list in Java, Python, Go, and JavaScript:

Java

public class Node {
    int data;
    Node next;

    Node(int d) {
        data = d;
        next = null;
    }
}

public class LinkedList {
    Node head; // head of list

    public void append(int data) {
        Node newNode = new Node(data);
        if (head == null) {
            head = newNode;
            return;
        }

        Node last = head;
        while (last.next != null) {
            last = last.next;
        }
        last.next = newNode;
    }
}

Python

class Node:
    def __init__(self, data=None):
        self.data = data
        self.next = None

class LinkedList:
    def __init__(self):
        self.head = None

    def append(self, data):
        if not self.head:
            self.head = Node(data)
        else:
            curr = self.head
            while curr.next:
                curr = curr.next
            curr.next = Node(data)

Go Lang

type Node struct {
    Data int
    Next *Node
}

type LinkedList struct {
    Head *Node
}

func (list *LinkedList) Append(data int) {
    newNode := &Node{Data: data}
    if list.Head == nil {
        list.Head = newNode
        return
    }

    last := list.Head
    for last.Next != nil {
        last = last.Next
    }
    last.Next = newNode
}

JavaScript

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

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

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

Conclusion

Singly-linked lists, and linked lists in general, are versatile and useful tools in a programmer’s data structure toolkit. They offer a dynamic and flexible way to handle data in a way that can be more efficient than static data structures like arrays, particularly for specific use cases.

Understanding linked lists and how to effectively implement them can greatly expand a developer’s ability to handle complex data problems, making them a key concept in computer science and software development.

Dive into this insightful post on CodingReflex to unlock the power of Quarkus, Java’s revolutionary framework for building ultra-speed applications.

  • For real-time updates and insights, follow our tech enthusiast and expert, Maulik, on Twitter.
  • Explore a universe of knowledge, innovation, and growth on our homepage, your one-stop resource for everything tech-related.