Open In App

Singly Linked List Tutorial

Last Updated : 28 Aug, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

A singly linked list is a fundamental data structure, it consists of nodes where each node contains a data field and a reference to the next node in the linked list. The next of the last node is null, indicating the end of the list. Linked Lists support efficient insertion and deletion operations.

Singly-Linked-List

Understanding Node Structure

In a singly linked list, each node consists of two parts: data and a pointer to the next node. This structure allows nodes to be dynamically linked together, forming a chain-like sequence.

C++
// Definition of a Node in a singly linked list
class Node {
  
public:
    // Data part of the node
    int data;

    // Pointer to the next node in the list
    Node* next;

    // Constructor to initialize the node with data
    Node(int data) {
        this->data = data;
        this->next = NULL;
    }
};
Java
// Definition of a Node in a singly linked list
public class Node {
    
    // Data part of the node
    int data;
    
    // Pointer to the next node in the list
    Node next;

    // Constructor to initialize the node with data
    public Node(int data){
        this.data = data;
        this.next = null;
    }
}
Python
# Definition of a Node in a singly linked list
class Node:
    def __init__(self, data):
        
        # Data part of the node
        self.data = data   
        self.next = None    
C#
// Definition of a Node in a singly linked list
public class Node{
    public int data;
    public Node next;

    // Constructor to initialize the node with data
    public Node(int data){
        this.data = data;
        this.next = null;
    }
}
JavaScript
// Definition of a Node in a singly linked list
class Node {
    constructor(data) {
        
        // Data part of the node
        this.data = data; 
        
        // Pointer to the next node
        this.next = null;   
    }
}

In this example, the Node class contains an integer data field (data) to store the information and a pointer to another Node (next) to establish the link to the next node in the list.

Creating an Example Linked List of Size 3 to Understand Working

Create the first node

  • Allocate memory for the first node and Store data in it.
  • Mark this node as head.

Create the second node

  • Allocate memory for the second node and Store data in it.
  • Link the first node’s next to this new node.

Create the third node

  • Allocate memory for the third node and Store data in it.
  • Link the second node’s next to this node.
  • Set its next to NULL to ensure that the next of the last is NULL.
C++
int main() {
    // Create the first node (head of the list)
    Node* head = new Node(10);

    // Link the second node
    head->next = new Node(20);

    // Link the third node
    head->next->next = new Node(30);

    // Link the fourth node
    head->next->next->next = new Node(40);
}
Java
public static void main(String[] args) {
        // Create the first node (head of the list)
        Node head = new Node(10);

        // Link the second node
        head.next = new Node(20);

        // Link the third node
        head.next.next = new Node(30);

        // Link the fourth node
        head.next.next.next = new Node(40);
    }
}
Python
if __name__ == "__main__":
    # Create the first node (head of the list)
    head = Node(10)

    # Link the second node
    head.next = Node(20)

    # Link the third node
    head.next.next = Node(30)

    # Link the fourth node
    head.next.next.next = Node(40)
C#
public static void Main(string[] args){
        // Create the first node (head of the list)
        Node head = new Node(10);

        // Link the second node
        head.next = new Node(20);

        // Link the third node
        head.next.next = new Node(30);

        // Link the fourth node
        head.next.next.next = new Node(40);

}
JavaScript
// Create the first node (head of the list)
let head = new Node(10);

// Link the second node
head.next = new Node(20);

// Link the third node
head.next.next = new Node(30);

// Link the fourth node
head.next.next.next = new Node(40);

Applications of Linked List


Advantage

  • Dynamic size (no fixed limit like arrays)
  • Efficient insertion and deletion (especially in the middle)
  • Can implement complex data structures like stack, queue, graph

Disadvantage

  • Extra memory required for storing pointers
  • No direct/random access (need traversal)
  • Cache unfriendly (not stored in contiguous memory)

Common Operation in Linked List

A linked list supports several operations. Here are the most common ones:



Article Tags :

Explore