Learn Basic Linked List Coding Questions with simple explanations and beginner-friendly code examples. Perfect for students and interview preparation.
Basic Linked List Coding Questions are designed to help beginners master one of the most fundamental data structures in programming. A linked list is a collection of nodes where each node contains data and a reference (or pointer) to the next node in the sequence. Unlike arrays, linked lists allow dynamic memory allocation, making them highly useful for scenarios where the size of the data can change frequently.
These coding questions cover all the essential operations of a linked list, such as:
- Creating a linked list: Understanding how to initialize nodes and link them together.
- Insertion: Adding new nodes at the beginning, middle, or end of the list.
- Deletion: Removing nodes safely without breaking the chain.
- Traversal: Accessing each node to perform operations like printing data or finding elements.
- Searching: Locating a specific value within the list.
- Counting nodes: Determining the size of the list dynamically.
Practicing these questions builds a strong foundation in pointers, memory management, and dynamic data structures, which are crucial for coding interviews, competitive programming, and real-world software development.
This guide focuses on beginner-friendly explanations and clean code examples, making it easy to follow step by step. By solving these questions, you not only learn the mechanics of linked lists but also improve problem-solving skills, logical thinking, and confidence in handling more complex data structures in the future.
Whether you are a student, a beginner programmer, or preparing for interviews, mastering basic linked list coding questions is the first step toward becoming a proficient programmer.
Creation & Traversal
- Create a singly linked list
- Traverse and print a linked list
- Count number of nodes in a linked list
- Search an element in a linked list
- Find length of linked list (iterative & recursive)
Insertion
- Insert at head
- Insert at tail
- Insert at a given position
- Insert after a given value
- Insert before a given value
- Insert in the middle of linked list
Deletion
- Delete head node
- Delete tail node
- Delete node at a given position
- Delete node by value
- Delete entire linked list
Basic Pointer Logic
- Find middle of linked list
- Find Nth node from beginning
- Find Nth node from end
- Check if linked list is empty
Reversal
- Reverse linked list (iterative)
- Reverse linked list (recursive)
- Reverse first K nodes
Two Pointer / Slow-Fast
- Detect loop in linked list
- Find starting point of loop
- Remove loop from linked list
Comparison & Checking
- Check if linked list is palindrome
- Compare two linked lists
- Check if two linked lists intersect
Sorting & Merging
- Merge two sorted linked lists
- Sort a linked list
- Remove duplicates from sorted list
- Remove duplicates from unsorted list
Interview-Famous Basics
- Add two numbers represented by linked lists
- Rotate linked list
- Pairwise swap nodes
- Delete alternate nodes
- Move last node to front
MUST-DO (Very Important)
If you do only these 10, you’re good for interviews:
1. Insert at head
2. Insert at tail
3. Insert at position
4. Delete at position
5. Reverse linked list
6. Find middle
7. Nth node from end
8. Detect loop
9. Remove duplicates
10. Merge two sorted lists
Creation & Traversal based Linked List Coding Questions
1. Create a singly linked list
#include <iostream>
using namespace std;
// Node structure
class Node {
public:
int data;
Node* next;
// Constructor
Node(int data) {
this->data = data;
this->next = NULL;
}
};
// Insert at Tail
void insertAtTail(Node* &head, int data) {
Node* newNode = new Node(data);
// If list is empty
if (head == NULL) {
head = newNode;
return;
}
Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
// Display Linked List
void printList(Node* head) {
Node* temp = head;
while (temp != NULL) {
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "NULL";
}
// Main function
int main() {
Node* head = NULL;
insertAtTail(head, 10);
insertAtTail(head, 20);
insertAtTail(head, 30);
insertAtTail(head, 40);
cout << "Singly Linked List: ";
printList(head);
return 0;
}
2.Traverse and Print a Linked List in C++
#include <iostream>
using namespace std;
// Node structure
class Node {
public:
int data;
Node* next;
Node(int data) {
this->data = data;
this->next = NULL;
}
};
// Traverse and print function
void traverseAndPrint(Node* head) {
Node* temp = head;
while (temp != NULL) {
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "NULL";
}
// Main function
int main() {
// Creating linked list manually
Node* head = new Node(10);
head->next = new Node(20);
head->next->next = new Node(30);
cout << "Linked List: ";
traverseAndPrint(head);
return 0;
}
3.Count Number of Nodes in a Linked List (C++)
#include <iostream>
using namespace std;
// Node structure
class Node {
public:
int data;
Node* next;
Node(int data) {
this->data = data;
this->next = NULL;
}
};
// Function to count nodes
int countNodes(Node* head) {
int count = 0;
Node* temp = head;
while (temp != NULL) {
count++;
temp = temp->next;
}
return count;
}
// Main function
int main() {
Node* head = new Node(10);
head->next = new Node(20);
head->next->next = new Node(30);
head->next->next->next = new Node(40);
cout << "Number of nodes: " << countNodes(head);
return 0;
}
4.Search an element in a linked list
#include <iostream>
using namespace std;
// Node structure
class Node {
public:
int data;
Node* next;
Node(int data) {
this->data = data;
this->next = NULL;
}
};
// Search function
bool searchElement(Node* head, int key) {
Node* temp = head;
while (temp != NULL) {
if (temp->data == key) {
return true; // Element found
}
temp = temp->next;
}
return false; // Element not found
}
// Main function
int main() {
Node* head = new Node(10);
head->next = new Node(20);
head->next->next = new Node(30);
head->next->next->next = new Node(40);
int key = 30;
if (searchElement(head, key))
cout << "Element found in the linked list";
else
cout << "Element not found in the linked list";
return 0;
}
Recursive Search
bool searchRecursive(Node* head, int key) {
if (head == NULL)
return false;
if (head->data == key)
return true;
return searchRecursive(head->next, key);
}
5.Find length of linked list (iterative & recursive)
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int data) {
this->data = data;
this->next = NULL;
}
};
int lengthIterative(Node* head) {
int count = 0;
Node* temp = head;
while (temp != NULL) {
count++;
temp = temp->next;
}
return count;
}
Recursive Method
int lengthRecursive(Node* head) {
if (head == NULL)
return 0;
return 1 + lengthRecursive(head->next);
}
Insertion based Linked List Coding Questions
1.Insert at head
#include <iostream>
using namespace std;
// Node structure
class Node {
public:
int data;
Node* next;
Node(int data) {
this->data = data;
this->next = NULL;
}
};
// Insert at head function
void insertAtHead(Node* &head, int data) {
Node* newNode = new Node(data); // Step 1: create new node
newNode->next = head; // Step 2: point to current head
head = newNode; // Step 3: update head
}
// Print linked list
void printList(Node* head) {
Node* temp = head;
while (temp != NULL) {
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "NULL" << endl;
}
// Main function
int main() {
Node* head = NULL; // empty linked list
insertAtHead(head, 10);
insertAtHead(head, 20);
insertAtHead(head, 30);
cout << "Linked List after inserting at head: ";
printList(head);
return 0;
}
2.Insert at Tail in a Linked List
#include <iostream>
using namespace std;
// Node structure
class Node {
public:
int data;
Node* next;
Node(int data) {
this->data = data;
this->next = NULL;
}
};
// Insert at tail function
void insertAtTail(Node* &head, int data) {
Node* newNode = new Node(data);
if (head == NULL) { // If list is empty
head = newNode;
return;
}
Node* temp = head;
while (temp->next != NULL) { // Traverse to last node
temp = temp->next;
}
temp->next = newNode; // Link last node to new node
}
// Print linked list
void printList(Node* head) {
Node* temp = head;
while (temp != NULL) {
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "NULL" << endl;
}
// Main function
int main() {
Node* head = NULL; // empty list
insertAtTail(head, 10);
insertAtTail(head, 20);
insertAtTail(head, 30);
cout << "Linked List after inserting at tail: ";
printList(head);
return 0;
}
3.Insert node at a given position
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
// Delete node at given position (0-based index)
Node* deleteAtPosition(Node* head, int position) {
if (head == NULL)
return head;
// Case 1: Delete head node
if (position == 0) {
Node* temp = head;
head = head->next;
delete temp;
return head;
}
Node* current = head;
// Traverse to (position - 1)
for (int i = 0; i < position - 1 && current->next != NULL; i++) {
current = current->next;
}
// If position is valid
if (current->next != NULL) {
Node* temp = current->next;
current->next = temp->next;
delete temp;
}
return head;
}
4.Delete Node by Value
Node* deleteByValue(Node* head, int value) {
if (head == NULL)
return head;
// Case 1: Value at head
if (head->data == value) {
Node* temp = head;
head = head->next;
delete temp;
return head;
}
Node* current = head;
// Search for the value
while (current->next != NULL && current->next->data != value) {
current = current->next;
}
// If value found
if (current->next != NULL) {
Node* temp = current->next;
current->next = temp->next;
delete temp;
}
return head;
}
5.Delete Entire Linked List
void deleteEntireList(Node*& head) {
Node* current = head;
while (current != NULL) {
Node* temp = current;
current = current->next;
delete temp;
}
head = NULL;
}
FAQ : Linked List Coding Questions
1. What is a linked list in simple words?
A linked list is a data structure where elements are stored in separate nodes, and each node points to the next one instead of being stored in continuous memory.
2. Why should I learn linked lists?
Linked lists help you understand dynamic memory, pointers, and real-world data structure concepts that are important for coding interviews and advanced programming.
3. What are the basic operations in a linked list?
The basic operations include insertion, deletion, traversal, searching, and counting nodes in the list.
4. Is a linked list better than an array?
It depends on the use case. Linked lists are better for frequent insertions and deletions, while arrays are faster for accessing elements by index.
5. What is a node in a linked list?
A node is a single unit of a linked list that stores data and a pointer (or reference) to the next node.
6. What is the difference between singly and doubly linked lists?
A singly linked list points only to the next node, while a doubly linked list points to both the previous and the next nodes.
7. Are linked list coding questions difficult for beginners?
No, basic linked list questions are easy if you understand pointers and practice step by step with simple examples.
8. Which language is best for learning linked lists?
C and C++ are commonly used because they clearly show pointer concepts, but Java and Python are also good options.
9. What are the most common linked list interview questions?
Common questions include reversing a linked list, finding the middle node, detecting a loop, and deleting a node at a given position.
10. How can I avoid mistakes while coding linked lists?
Always check for null pointers, handle edge cases like empty lists, and test your code with small inputs.
11. How much practice is needed to master linked lists?
With regular practice for a few days and solving basic problems, you can become comfortable with linked list concepts.
12. Are linked lists still used in real projects?
Yes, linked lists are used internally in operating systems, memory management, and many software libraries.
Read More : FPGA Interview Questions & Answers
Mr. Raj Kumar is a highly experienced Technical Content Engineer with 7 years of dedicated expertise in the intricate field of embedded systems. At Embedded Prep, Raj is at the forefront of creating and curating high-quality technical content designed to educate and empower aspiring and seasoned professionals in the embedded domain.
Throughout his career, Raj has honed a unique skill set that bridges the gap between deep technical understanding and effective communication. His work encompasses a wide range of educational materials, including in-depth tutorials, practical guides, course modules, and insightful articles focused on embedded hardware and software solutions. He possesses a strong grasp of embedded architectures, microcontrollers, real-time operating systems (RTOS), firmware development, and various communication protocols relevant to the embedded industry.
Raj is adept at collaborating closely with subject matter experts, engineers, and instructional designers to ensure the accuracy, completeness, and pedagogical effectiveness of the content. His meticulous attention to detail and commitment to clarity are instrumental in transforming complex embedded concepts into easily digestible and engaging learning experiences. At Embedded Prep, he plays a crucial role in building a robust knowledge base that helps learners master the complexities of embedded technologies.
