Singly Linked List Tutorial
Last Updated :
05 Oct, 2025
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.
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++
#include<iostream>
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
while (head != nullptr) {
cout << head->data << " ";
head = head->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
while (head != null) {
System.out.print(head.data + " ");
head = head.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 Program {
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
while (head != null) {
Console.Write(head.data + " ");
head = head.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
- Dynamic size (no fixed limit like arrays)
- Efficient insertion and deletion at beginning and end. We also have insertion at the middle efficient if we have reference or pointer to the node after which we need to insert.
- 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)
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem