Singly Linked List
A Singly Linked List is a simple and dynamic linear data structure in which elements are stored in separate nodes connected through links. Each node contains data and the address of the next node. Unlike arrays, nodes in a linked list are not stored in contiguous memory locations.
The singly linked list allows traversal in only one direction, from the first node to the last node.
Linked lists are widely used when the size of data changes frequently during program execution. In arrays, insertion and deletion are costly because elements need to be shifted. Singly linked lists solve this problem by connecting nodes using pointers.
In a singly linked list:
- The first node is called the head
- The last node points to
NULL - Traversal is possible only in forward direction
Because of its simple structure and efficient memory usage, singly linked lists are one of the most important data structures in computer science.
Structure of Singly Linked List
Each node in a singly linked list contains two parts:
| Part | Description |
|---|---|
| Data | Stores the actual value |
| Next Pointer | Stores address of next node |
Example
[10 | * ] → [20 | * ] → [30 | NULL]Here:
10,20, and30are data values- The pointer connects nodes together
NULLindicates the end of the list
A singly linked list node is commonly represented using structure in C language.
C Structure
struct Node {
int data;
struct Node* next;
};Here:
datastores the valuenextstores the address of next node
Features of Singly Linked List
- Dynamic memory allocation
- Efficient insertion and deletion
- Memory is utilized as needed
- Nodes can be stored anywhere in memory
- Traversal possible only in one direction
Operations on Singly Linked List
The common operations performed on singly linked list are:
- Creation
- Traversal
- Insertion
- Deletion
- Searching
- Display
1. Creation of Singly Linked List
Creation means making a new node and assigning data to it. Initially, the next pointer is set to NULL.
Algorithm
CREATE_NODE(value)
1. Allocate memory for new node
2. Store value in node
3. Set next = NULL
4. Return nodeC Program
struct Node* createNode(int value) {
struct Node* newNode =
(struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
return newNode;
}Example
[10 | NULL]2. Traversal Operation
Traversal means visiting each node one by one from the beginning to the end.
Traversal is used to:
- Display nodes
- Count nodes
- Access all elements
Example
10 → 20 → 30 → NULLTraversal Output:
10 20 30Algorithm
TRAVERSE(head)
1. temp = head
2. While temp ≠ NULL
Print temp.data
temp = temp.next
3. StopC Program
void traverse() {
struct Node* temp = head;
while(temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
}3. Insertion Operation
Insertion means adding a new node into the linked list.
Insertion can be performed:
- At beginning
- At end
- At specific position
A. Insertion at Beginning
In this method, a new node becomes the first node of the list.
Example
Before insertion:
20 → 30 → NULLAfter inserting 10:
10 → 20 → 30 → NULLAlgorithm
INSERT_BEGIN(head, value)
1. Create new node
2. new.next = head
3. head = newC Program
void insertBegin(int value) {
struct Node* newNode =
createNode(value);
newNode->next = head;
head = newNode;
}B. Insertion at End
In this method, the new node is added after the last node.
Example
Before insertion:
10 → 20 → NULLAfter inserting 30:
10 → 20 → 30 → NULLAlgorithm
INSERT_END(head, value)
1. Create new node
2. Traverse to last node
3. last.next = new nodeC Program
void insertEnd(int value) {
struct Node* newNode =
createNode(value);
if(head == NULL) {
head = newNode;
return;
}
struct Node* temp = head;
while(temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}4. Deletion Operation
Deletion removes nodes from the linked list.
Deletion can occur:
- From beginning
- From end
- From specific position
A. Delete from Beginning
The first node is removed and the head moves to the next node.
Example
Before deletion:
10 → 20 → 30 → NULLAfter deletion:
20 → 30 → NULLAlgorithm
DELETE_BEGIN(head)
1. temp = head
2. head = head.next
3. Delete tempC Program
void deleteBegin() {
if(head == NULL)
return;
struct Node* temp = head;
head = head->next;
free(temp);
}B. Delete from End
The last node is removed from the linked list.
Example
Before deletion:
10 → 20 → 30 → NULLAfter deletion:
10 → 20 → NULLAlgorithm
DELETE_END(head)
1. Move to second-last node
2. Delete last node
3. Set next = NULLC Program
void deleteEnd() {
if(head == NULL)
return;
if(head->next == NULL) {
free(head);
head = NULL;
return;
}
struct Node* temp = head;
while(temp->next->next != NULL) {
temp = temp->next;
}
free(temp->next);
temp->next = NULL;
}5. Searching Operation
Searching means finding whether an element exists in the linked list.
Since linked lists do not support direct indexing, nodes are checked one by one.
Example
10 → 20 → 30 → 40 → NULLSearch key = 30
Result:
- Element Found
Algorithm
SEARCH(head, key)
1. temp = head
2. While temp ≠ NULL
If temp.data == key
Return Found
temp = temp.next
3. Return Not FoundC Program
void search(int key) {
struct Node* temp = head;
while(temp != NULL) {
if(temp->data == key) {
printf("Element Found");
return;
}
temp = temp->next;
}
printf("Element Not Found");
}6. Display Operation
Display operation prints all nodes of the linked list sequentially.
Example
15 → 25 → 35 → NULLDisplay Output:
15 25 35Algorithm
DISPLAY(head)
1. Start from head
2. Print each node
3. Move to next node
4. Stop at NULLC Program
void display() {
struct Node* temp = head;
while(temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL");
}Time Complexity of Singly Linked List Operations
| Operation | Time Complexity |
|---|---|
| Traversal | O(n) |
| Insertion at Beginning | O(1) |
| Insertion at End | O(n) |
| Deletion at Beginning | O(1) |
| Deletion at End | O(n) |
| Searching | O(n) |
Advantages of Singly Linked List
- Dynamic size
- Efficient insertion and deletion
- Better memory utilization
- Easy implementation
Disadvantages of Singly Linked List
- Sequential access only
- Extra memory required for pointers
- Backward traversal not possible
- Searching is slower than arrays
Applications of Singly Linked List
Singly linked lists are used in,
- Music playlists
- Browser history
- Dynamic memory management
- Stack implementation
- Queue implementation
Conclusion
A singly linked list is one of the most fundamental and important data structures in computer science. It stores data dynamically using nodes connected through pointers. Because of its flexible structure, it supports efficient insertion and deletion operations. Understanding singly linked lists is important before learning advanced data structures such as doubly linked lists, circular linked lists, trees, and graphs.