00:00

Singly Linked List Data Structure

Last updated:

The singly linked list is the simplest kind of linked list: every node points only to the node after it. This page explains its structure, the operations it supports, a complete Java implementation, and where it is a good or a poor choice.

What is Singly Linked List?

A singly linked list is a sequence of nodes in which each node knows only its successor. A node has two fields: the data it stores and a next reference to the following node.

The list is accessed through a head reference to the first node; when the list is empty, head is null. The last node (the tail) has next == null, which is how a loop knows it has reached the end.

Singly linked list: head points to the first node, each node points to the next, and the last node points to null

Because there is no link back to the previous node, a singly linked list can only be walked from front to back.

Operations on Singly Linked List

The following operations can be performed on a singly linked list.

Insertion

A new node can be added at the following positions of the singly linked list.

  • Insert a node at the beginning
  • Insert a node at the end
  • Insert a node at a specific position

Deletion

An existing node can be deleted from the following positions of the singly linked list.

  • Delete a node at the beginning
  • Delete a node at the end
  • Delete a node at a specific position

Traversal

Traversal can be performed only in the forward direction. Start from the head and move through each node using the next pointers till the last node of the singly linked list.

Searching

We can search for a specific node in the singly linked list by comparing each node's data with the key.

Singly Linked List implementation in Java

SinglyLinkedList.java
// Java program to implement a Singly Linked List
class Node {
  public int data;
  public Node next;
  public Node(int data) {
    this.data = data;
    this.next = null;
  }
}

public class SinglyLinkedList {
  // Create a head node which will point to the first node of the list
  Node head;

  // Method to insert a node in the list
  public void insert(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;
  }

  // Method to print list
  public void printList() {
    Node node = head;
    while (node != null) {
      System.out.print(node.data + " ");
      node = node.next;
    }
  }

  public static void main(String[] args) {
    SinglyLinkedList list = new SinglyLinkedList();

    // Insert elements in the list
    list.insert(10);
    list.insert(20);
    list.insert(30);
    list.insert(40);
    list.insert(50);

    // Print list elements
    System.out.print("Singly Linked List elements: ");
    list.printList();
  }
}
Output
Singly Linked List elements: 10 20 30 40 50

In the above example, the Node class represents a single element of the linked list, and the SinglyLinkedList class is used to create a linked list, insert a node in the list, and display the elements of the linked list.

Advantages of Singly Linked List

  • No fixed capacity: nodes are created only when values are added.
  • O(1) insertion and deletion at the head, with no shifting of other elements.
  • Less memory per node than a doubly linked list (one reference instead of two).
  • Short, simple code, which makes it a good base for stacks, queues and hash-table chains.

Disadvantages of Singly Linked List

The single forward link also brings some limits:

  • No random access: reaching the k-th node means following k links.
  • Each node carries a reference and an object header, so it uses more memory than an array of the same values.
  • No backward traversal, so deleting the last node or the node before a given one requires a walk from the head (O(n)).
  • Nodes are scattered in memory, which makes traversal slower in practice than looping over an array.