Interview Prep
Interview questions, roadmaps and practice sets
Tutorials
Free tutorials on 30+ topics
Blog
Career guides, skills and interview tips
Free PDFs
Notes, cheat sheets and question banks
Certificates
Free skill tests and course certificates
Tools & Whiteboard
Free tools you can use right in the browser
Practice Tests
Mock tests, MCQs and quick Q&A
Free LMS
Learn on the Uncodemy LMS
Choose the best training program according to your career.
Improve the performance of your business with our innovative Industrial Training Program.
Read More →
Our instructor-led training classes are designed to make you an expert.
Read More →
Real-life case studies are a great way to demonstrate your practical knowledge.
Read More →
Explore blogs, latest technologies and industry updates with practical examples.
Read More →Browse all available training programs and choose the right one for your career.
Read More →Know more about who we are and what we stand for.
Discover our mission to equip individuals with essential IT skills and industry expertise.
Read More →
Free tutorials, interview questions, tools and calculators to help you learn.
Explore →Learn, practice and plan your career — bilkul free.
Book a free video counselling session and pick the right course for you.
Read More →A singly linked list is usually the first data structure where C students really have to use pointers and malloc(). Once you understand it, stacks, queues, trees and graphs become much easier.

This guide builds a linked list step by step and ends with one complete program you can compile and run.
A singly linked list is a chain of nodes. Each node stores:
The first node is called the head. The last node’s next pointer is NULL, which marks the end.
head -> [10 | •] -> [20 | •] -> [30 | NULL]
Unlike an array, nodes are not stored next to each other in memory. Each node can be anywhere; the pointers connect them.
| Point | Array | Singly linked list |
|---|---|---|
| Size | Fixed when created (for static arrays) | Grows and shrinks at run time |
| Access to i-th element | O(1) | O(n) — must walk from head |
| Insert at beginning | O(n) — shift elements | O(1) |
| Extra memory | None | One pointer per node |
| Memory layout | Continuous | Scattered |
Use a linked list when you insert and delete often and rarely need random access.
struct Node {
int data;
struct Node *next;
};
struct Node *create_node(int value) {
struct Node *n = malloc(sizeof(struct Node));
if (n == NULL) {
printf("Memory allocation failed\n");
exit(1);
}
n->data = value;
n->next = NULL;
return n;
}
Always check if malloc() returned NULL. It rarely happens on a laptop, but it is a good habit and examiners like it.
struct Node **headFunctions that may change the head (insert at beginning, delete first node, reverse) receive a pointer to the head pointer. If you pass struct Node *head instead, the function changes only its local copy and the list in main() never changes. This is the single most common bug in student code.
Insert at the beginning, O(1) — the new node points to the old head, and becomes the new head.
void insert_at_beginning(struct Node **head, int value) {
struct Node *n = create_node(value);
n->next = *head;
*head = n;
}
Insert at the end, O(n) — walk to the last node and attach the new node.
void insert_at_end(struct Node **head, int value) {
struct Node *n = create_node(value);
if (*head == NULL) {
*head = n;
return;
}
struct Node *temp = *head;
while (temp->next != NULL)
temp = temp->next;
temp->next = n;
}
Insert at a position (1 = first). If the position is larger than the list, the node is added at the end.
void insert_at_position(struct Node **head, int value, int pos) {
if (pos <= 1 || *head == NULL) {
insert_at_beginning(head, value);
return;
}
struct Node *temp = *head;
for (int i = 1; i < pos - 1 && temp->next != NULL; i++)
temp = temp->next;
struct Node *n = create_node(value);
n->next = temp->next;
temp->next = n;
}
The order of the last two lines matters: first connect the new node to the rest of the list, then connect the previous node to the new node. Doing it the other way round loses the rest of the list.
void delete_value(struct Node **head, int value) {
struct Node *temp = *head, *prev = NULL;
while (temp != NULL && temp->data != value) {
prev = temp;
temp = temp->next;
}
if (temp == NULL) {
printf("%d not found\n", value);
return;
}
if (prev == NULL)
*head = temp->next; /* deleting the first node */
else
prev->next = temp->next;
free(temp);
}
We keep a prev pointer because in a singly linked list you cannot go backwards.
int search(struct Node *head, int value) {
int pos = 1;
while (head != NULL) {
if (head->data == value)
return pos;
head = head->next;
pos++;
}
return -1;
}
int count_nodes(struct Node *head) {
int c = 0;
while (head != NULL) {
c++;
head = head->next;
}
return c;
}
void display(struct Node *head) {
while (head != NULL) {
printf("%d -> ", head->data);
head = head->next;
}
printf("NULL\n");
}
These functions don’t change the list, so a normal struct Node *head is enough.
Reversing is a favourite interview question. We walk through the list once and flip each next pointer.
void reverse(struct Node **head) {
struct Node *prev = NULL, *curr = *head, *next = NULL;
while (curr != NULL) {
next = curr->next; /* save the rest of the list */
curr->next = prev; /* flip the pointer */
prev = curr;
curr = next;
}
*head = prev;
}
Time O(n), extra space O(1).
Every malloc() needs a free(). Before the program ends, release all nodes:
void free_list(struct Node **head) {
struct Node *temp;
while (*head != NULL) {
temp = *head;
*head = (*head)->next;
free(temp);
}
}
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};
struct Node *create_node(int value) {
struct Node *n = malloc(sizeof(struct Node));
if (n == NULL) {
printf("Memory allocation failed\n");
exit(1);
}
n->data = value;
n->next = NULL;
return n;
}
void insert_at_beginning(struct Node **head, int value) {
struct Node *n = create_node(value);
n->next = *head;
*head = n;
}
void insert_at_end(struct Node **head, int value) {
struct Node *n = create_node(value);
if (*head == NULL) {
*head = n;
return;
}
struct Node *temp = *head;
while (temp->next != NULL)
temp = temp->next;
temp->next = n;
}
void insert_at_position(struct Node **head, int value, int pos) {
if (pos <= 1 || *head == NULL) {
insert_at_beginning(head, value);
return;
}
struct Node *temp = *head;
for (int i = 1; i < pos - 1 && temp->next != NULL; i++)
temp = temp->next;
struct Node *n = create_node(value);
n->next = temp->next;
temp->next = n;
}
void delete_value(struct Node **head, int value) {
struct Node *temp = *head, *prev = NULL;
while (temp != NULL && temp->data != value) {
prev = temp;
temp = temp->next;
}
if (temp == NULL) {
printf("%d not found\n", value);
return;
}
if (prev == NULL)
*head = temp->next;
else
prev->next = temp->next;
free(temp);
}
int search(struct Node *head, int value) {
int pos = 1;
while (head != NULL) {
if (head->data == value)
return pos;
head = head->next;
pos++;
}
return -1;
}
int count_nodes(struct Node *head) {
int c = 0;
while (head != NULL) {
c++;
head = head->next;
}
return c;
}
void reverse(struct Node **head) {
struct Node *prev = NULL, *curr = *head, *next = NULL;
while (curr != NULL) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
*head = prev;
}
void display(struct Node *head) {
while (head != NULL) {
printf("%d -> ", head->data);
head = head->next;
}
printf("NULL\n");
}
void free_list(struct Node **head) {
struct Node *temp;
while (*head != NULL) {
temp = *head;
*head = (*head)->next;
free(temp);
}
}
int main(void) {
struct Node *head = NULL;
insert_at_end(&head, 20);
insert_at_end(&head, 30);
insert_at_beginning(&head, 10);
insert_at_position(&head, 25, 3);
printf("List: ");
display(head);
printf("Nodes: %d\n", count_nodes(head));
printf("Position of 30: %d\n", search(head, 30));
delete_value(&head, 25);
printf("After deleting 25: ");
display(head);
delete_value(&head, 99);
reverse(&head);
printf("Reversed: ");
display(head);
free_list(&head);
printf("After free: ");
display(head);
return 0;
}
Compile and run:
gcc -Wall -Wextra -o linkedlist linkedlist.c
./linkedlist
Output
List: 10 -> 20 -> 25 -> 30 -> NULL
Nodes: 4
Position of 30: 4
After deleting 25: 10 -> 20 -> 30 -> NULL
99 not found
Reversed: 30 -> 20 -> 10 -> NULL
After free: NULL
| Operation | Time |
|---|---|
| Insert at beginning | O(1) |
| Insert at end (without tail pointer) | O(n) |
| Insert at position | O(n) |
| Delete by value | O(n) |
| Search | O(n) |
| Reverse | O(n) |
Tip: if you keep an extra tail pointer, insert at end also becomes O(1).
Imagine a student who writes void insert(struct Node *head, int value), adds three numbers in main(), and then prints the list — only to see NULL. The function clearly runs; a printf inside it even shows the new node. So where did the nodes go?
They went into the function’s own copy of head. When the function ended, that copy disappeared, and main()’s head was still NULL (and the nodes became a memory leak). Changing the parameter to struct Node **head and calling insert(&head, 10) fixes it. Almost everyone who learns linked lists in C hits this once — and after fixing it, pointers to pointers finally make sense.
head by value when the function must change it (see above).temp->next before saving it.NULL, e.g. temp->next when temp is already NULL. Check the loop condition carefully.free(). Once freed, never read it again.free_list(). On Linux you can check with valgrind --leak-check=full ./linkedlist.tail pointer and make insert at end O(1).In a singly linked list each node points only to the next node. In a doubly linked list each node also points to the previous node, so you can move both ways, but each node uses more memory.
It tells the program where the list ends, so loops like while (temp != NULL) stop correctly.
Yes, using array indexes instead of pointers (a “static linked list”), but in C the usual way is with malloc() and pointers.
malloc and free in C: https://en.cppreference.com/w/c/memory/malloc