Linked List Questions and Solutions
Linked List Questions and Solutions
Introduction
A linked list is one of the most important topics in data structures and programming. It is widely used in memory management, dynamic data storage, stacks, queues, and many real-world algorithms. Unlike arrays, linked lists allow dynamic memory allocation, making insertion and deletion operations more efficient.
In this article, you will learn the most commonly asked linked list questions in C programming with simple and practical solutions. These examples cover insertion, deletion, searching, splitting, circular linked lists, loop detection, recursion, and more. If you are preparing for university exams, coding interviews, or improving your understanding of coding concepts, these questions will help you strengthen your fundamentals.
Before diving in, make sure you're comfortable with algorithm design fundamentals and graph traversal concepts like BFS and DFS, as linked list traversal patterns share similar logic.
What is a Linked List?
A linked list is a linear data structure where each element is stored inside a node. Every node contains:
Data
Pointer to the next node
Memory for each node is allocated dynamically using malloc(), which is part of <stdlib.h>.
Basic Node Structure in C
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
void printList(Node* head) {
Node* temp = head;
while(temp) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}Linked List Insertion Questions
1. Insert a Node Before the First Node
void insertBeforeFirst(Node** head, int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = *head;
*head = newNode;
}2. Insert a Node After the First Node
void insertAfterFirst(Node* head, int data) {
if (!head) return;
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = head->next;
head->next = newNode;
}3. Insert a Node After the Last Node
void insertAfterLast(Node* head, int data) {
Node* temp = head;
while(temp->next)
temp = temp->next;
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
temp->next = newNode;
}4. Insert a Node Before the Last Node
void insertBeforeLast(Node* head, int data) {
if (!head || !head->next) return;
Node* temp = head;
while(temp->next->next)
temp = temp->next;
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = temp->next;
temp->next = newNode;
}5. Search a Node and Insert Before and After It
void insertAroundValue(Node** head, int searchVal, int beforeData, int afterData) {
Node* temp = *head;
Node* prev = NULL;
while(temp && temp->data != searchVal) {
prev = temp;
temp = temp->next;
}
if (temp) {
Node* newBefore = (Node*)malloc(sizeof(Node));
newBefore->data = beforeData;
newBefore->next = temp;
if (prev) prev->next = newBefore;
else *head = newBefore;
Node* newAfter = (Node*)malloc(sizeof(Node));
newAfter->data = afterData;
newAfter->next = temp->next;
temp->next = newAfter;
}
}Linked List Deletion Questions
6. Delete the First Node
void deleteFirst(Node** head) {
if (*head) {
Node* temp = *head;
*head = (*head)->next;
free(temp);
}
}7. Delete the Last Node
void deleteLast(Node** head) {
if (!*head) return;
if (!(*head)->next) {
free(*head);
*head = NULL;
return;
}
Node* temp = *head;
while(temp->next->next)
temp = temp->next;
free(temp->next);
temp->next = NULL;
}8. Search and Delete a Node
void deleteValue(Node** head, int value) {
Node* temp = *head;
Node* prev = NULL;
while(temp && temp->data != value) {
prev = temp;
temp = temp->next;
}
if (!temp) return;
if (!prev) *head = temp->next;
else prev->next = temp->next;
free(temp);
}Linked List Splitting Questions
9. Split List into Odd and Even Nodes
void splitOddEven(Node* head, Node** odd, Node** even) {
while(head) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = head->data;
newNode->next = NULL;
if (head->data % 2 == 0) {
newNode->next = *even;
*even = newNode;
} else {
newNode->next = *odd;
*odd = newNode;
}
head = head->next;
}
}10. Split a Linked List at a Given Index
void splitAtIndex(Node* head, int index, Node** first, Node** second) {
*first = head;
int i = 0;
while(i < index - 1 && head) {
head = head->next;
i++;
}
if (head) {
*second = head->next;
head->next = NULL;
}
}Recursive and Reverse Display Questions
11. Display Linked List in Reverse Order
Recursion is a key technique here — if you want to strengthen your recursion fundamentals, check out Tower of Hanoi, a classic recursive problem.
void reverseDisplay(Node* head) {
if (!head) return;
reverseDisplay(head->next);
printf("%d ", head->data);
}Circular Linked List and Loop Questions
12. Convert a Linked List into a Circular Linked List
void convertToCircular(Node* head) {
if (!head) return;
Node* temp = head;
while(temp->next)
temp = temp->next;
temp->next = head;
}13. Detect a Loop Using Floyd's Cycle Detection Algorithm
Floyd's Cycle Detection (Tortoise and Hare) is one of the most elegant algorithms in computer science. It uses two pointers moving at different speeds to detect a cycle in O(n) time and O(1) space.
int detectLoop(Node* head) {
Node *slow = head, *fast = head;
while(fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
if (slow == fast)
return 1;
}
return 0;
}Linked List Practice Questions
14. Delete Nodes with Odd Values
void deleteOdd(Node** head) {
while(*head && (*head)->data % 2 != 0) {
Node* temp = *head;
*head = (*head)->next;
free(temp);
}
Node* curr = *head;
while(curr && curr->next) {
if (curr->next->data % 2 != 0) {
Node* temp = curr->next;
curr->next = curr->next->next;
free(temp);
} else {
curr = curr->next;
}
}
}15. Display Alternate Nodes
void displayAlternate(Node* head) {
int toggle = 1;
while(head) {
if (toggle) printf("%d ", head->data);
toggle = !toggle;
head = head->next;
}
printf("\n");
}16. Delete Alternate Nodes
void deleteAlternate(Node* head) {
while(head && head->next) {
Node* temp = head->next;
head->next = temp->next;
free(temp);
head = head->next;
}
}Stack and Queue Representation Using Linked List
Linked lists are the natural backing structure for stacks and queues. For a deeper look at how these structures are traversed in graphs, see BFS vs DFS — BFS uses a queue and DFS uses a stack, both often implemented as linked lists.
17. Print Stack Like Queue
void printStackAsQueue(Node* top) {
reverseDisplay(top);
}18. Print Queue Like Stack
void printQueueAsStack(Node* front) {
reverseDisplay(front);
}Double Pointer and Call by Reference Questions
19. Linked List Implementation Using Double Pointers
void addNodeDoublePtr(Node** head, int val) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = val;
newNode->next = NULL;
if (!*head) {
*head = newNode;
} else {
Node* temp = *head;
while(temp->next)
temp = temp->next;
temp->next = newNode;
}
}20. Call by Reference Linked List Insertion
void insertByRef(Node** head, int val) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = val;
newNode->next = *head;
*head = newNode;
}Conclusion
These linked list questions cover the most important operations used in data structures and algorithms. By practicing insertion, deletion, searching, splitting, recursion, and circular linked lists, you can build a strong foundation in C programming and improve your problem-solving skills.
These examples are especially useful for:
University practical exams
Viva preparation
Technical interviews
Coding practice
DSA revision
Once you've mastered linked lists, level up with more advanced data structures on NoteHub:
Segment Tree — range query powerhouse
Fenwick Tree (BIT) — prefix sums made elegant
Square Root Decomposition — practical range query technique
Mo's Algorithm — offline range queries using sqrt decomposition
You can also explore GeeksforGeeks' Linked List guide for additional problems and variations like doubly linked lists, circular doubly linked lists, and XOR linked lists.
