Singly Linked List Tutorial (original) (raw)

Last Updated : 13 Jan, 2026

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.

link1

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

Create the second node

Create the third node

#include using namespace std;

// singly linked list node structure class Node { public: int data; Node* next;

// constructor to initialize a new node with data
Node(int new_data) {
    this->data = new_data;
    this->next = nullptr;
}

};

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);

// printing linked list
Node* temp = head;
while (temp != nullptr) {
    cout << temp->data << " ";
    temp = temp->next;
}

}

Java

class Node { int data; Node next;

// constructor to initialize a new node with data
Node(int new_data) {
    this.data = new_data;
    this.next = null;
}

}

public class Main { 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);

    // printing linked list
    Node temp = head;
    while (temp != null) {
        System.out.print(temp.data + " ");
        temp = temp.next;
    }
}

}

Python

constructor to initialize a new node with data

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

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)

printing linked list

temp = head while temp is not None: print(temp.data, end=" ") temp = temp.next

C#

using System;

class Node { public int data; public Node next;

// constructor to initialize a new node with data
public Node(int new_data) {
    this.data = new_data;
    this.next = null;
}

}

class GFG { 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);

    // printing linked list
    Node temp = head;
    while (temp != null) {
        Console.Write(temp.data + " ");
        temp = temp.next;
    }
}

}

JavaScript

// constructor to initialize a new node with data class Node { constructor(new_data) { this.data = new_data; this.next = null; } }

// 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);

// printing linked list let temp = head; while (temp !== null) { process.stdout.write(temp.data + " "); temp = temp.next; }

`

Common Operation in Linked List

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

Applications of Linked List

Advantage

Disadvantage