Home »
Data Structure
Singly Linked List and Its Basic Operations with Traversing Implementation
Last Updated : November 24, 2025
What is a Singly Linked List?
A singly linked list is a linear data structure made up of nodes where each node stores data and a pointer to the next node. The last node points to NULL, indicating the end of the list.
Singly Linked List Node Structure
Each node of a linked list can be created using the struct keyword in C.
struct node{
// data field, can be of various types
int data;
// pointer to the next node
struct node* next;
};
Basic Operations on a Singly Linked List
These are the most commonly performed operations on a singly linked list:
- Traversing the list
- Inserting a node
- Deleting a node
- Searching an element
- Counting total nodes
How Traversing Works in a Singly Linked List
To traverse a linked list, follow these steps:
- Start from the head node
- Move through each node using the next pointer
- Display or process the data of the current node
- Stop when the pointer becomes NULL
C Program to Traverse a Linked List and Count Nodes
#include <stdio.h>
#include <stdlib.h>
struct node{
int data; // data field
struct node *next;
};
void traverse(struct node* head){
struct node* current=head;
int count=0;
printf("\n traversing the list\n");
while(current!=NULL){
count++;
printf("%d ",current->data);
current=current->next;
}
printf("\ntotal no of nodes : %d\n",count);
}
struct node* createnode(int d){
struct node* temp= (struct node*) malloc(sizeof(struct node));
temp->data=d;
temp->next=NULL;
return temp;
}
int main()
{
printf("\ncreating & traversing linked list\n");
struct node* head=createnode(5);
head->next=createnode(10);
head->next->next=createnode(20);
head->next->next->next=createnode(1);
traverse(head);
return 0;
}
Output
creating & traversing linked list
traversing the list
5 10 20 1
total no of nodes : 4
Time Complexity
O(n) — the list of size n is scanned once.
Space Complexity
O(1) — no extra memory is used apart from pointers.
Advertisement
Advertisement