A singly linked list is a linear data structure where each element (node) contains data and a pointer to the next node. Unlike arrays, nodes arenβt stored contiguously in memory, enabling dynamic memory usage and efficient insertions/deletions.
π‘ Real-world analogy: Think of a treasure hunt where each clue points to the next location.

Node Structure Explained
struct node {
int data // Data (int, char, etc.)
struct node* next; // Pointer to next node
};- data: Actual value (
int,char,struct, etc.) - next: Memory address of next node (or
NULLfor last node) - head: Pointer to first node (entry point)
Key Advantages & Disadvantages
| Pros | Cons |
|---|---|
| Dynamic memory allocation | No random access (e.g., <code class="language-c">arr[5]) |
O(1) insertion/deletion at head | Extra memory for pointers |
| Efficient for stacks/queues implementation | Traversal is O(n) |
π‘ Key Insight: Use linked lists when you need frequent insertions/deletions, dynamic sizing, or memory efficiency. Avoid them for random access tasks (e.g., binary search).
Singly Linked List β Core operations in C
1. Create a Node
struct node* createNode(int data) {
struct Node* newNode = (struct node*) malloc(sizeof(struct node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}2. Insert at Head β O(1)
void insertFront(struct node** head, int data) {
struct node* newNode = createNode(data);
newNode->next = *head;
*head = newNode;
}3. Insert at End β O(n)
void insertEnd(struct node** head, int data) {
struct node* newNode = createNode(data);
if (*head == NULL) {
// List is empty
*head = newNode;
} else {
struct node* temp = *head;
// Traverse till end of list
while (temp->next != NULL)
temp = temp->next;
temp->next = newNode;
}
}4. Delete First Node β O(1)
void deleteFront(struct node** head) {
if (*head == NULL) {
// List is empty, nothing to delete
return;
}
struct node* temp = *head;
*head = (*head)->next;
free(temp);
}5. Traverse List
void printList(struct node* head) {
while (head != NULL) {
printf("%d β ", head->data);
head = head->next;
}
printf("\n");
}Real-World Applications
- Undo/Redo: Stores editor states in text processors
- Hash Tables: Handles collisions using chaining
- Polynomial Representation: Stores coefficients and exponents
Singly vs. Doubly Linked Lists
| Feature | Singly Linked | Doubly Linked |
|---|---|---|
| Pointers per Node | 1 (next) | 2 (prev, next) |
| Memory Overhead | Lower | Higher |
| Traversal | Forward only | Bidirectional |
| Use Cases | Stacks, simple queues | Browser history, editors |
πββοΈ Doubly linked list: Pros/cons, applications, implementation.
πββοΈ Arrays vs. Linked Lists