Linked List Interview Questions
Linked lists are one of the most common topics in data structures and coding interviews. Interview questions often test whether you understand nodes, pointers, traversal, insertion, deletion, reversal, and linked list complexity.
More advanced questions usually combine linked lists with techniques such as two pointers, fast and slow pointers, recursion, and dummy nodes.
1. What Is a Linked List?
A linked list is a linear data structure made up of nodes. Each node stores data and a reference to another node.
10 → 20 → 30 → null
In a singly linked list, each node contains a value and a reference to the next node.
The last node points to null.
2. How Is a Linked List Different from an Array?
Arrays store elements in indexed positions, while linked lists connect nodes through references.
| Feature | Array | Linked List |
|---|---|---|
| Index access | O(1) | O(n) |
| Insert at beginning | O(n) | O(1) with head reference |
| Delete after known node | Usually O(n) | O(1) |
| Contiguous storage | Typically yes | No |
3. What Are the Types of Linked Lists?
The main types are:
- Singly linked list: each node points to the next node.
- Doubly linked list: each node points to both the previous and next nodes.
- Circular linked list: the final node points back to the first node.
- Circular doubly linked list: both directions form a circular structure.
4. How Do You Define a Linked List Node?
A singly linked list node normally contains a value and a reference to the next node.
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
5. How Do You Traverse a Linked List?
Start from the head and repeatedly follow the next reference until
reaching null.
function printList(head) {
let current = head;
while (current !== null) {
console.log(current.value);
current = current.next;
}
}
Traversing all nodes takes O(n) time.
6. How Do You Insert a Node at the Beginning?
Create a new node, point it to the current head, and then update the head.
function insertAtBeginning(head, value) {
const node = new Node(value);
node.next = head;
return node;
}
With a direct head reference, this operation takes O(1) time.
7. How Do You Insert a Node at the End?
Traverse to the last node and connect the new node to it. Without a tail reference, finding the last node takes O(n).
function append(head, value) {
const node = new Node(value);
if (head === null) {
return node;
}
let current = head;
while (current.next !== null) {
current = current.next;
}
current.next = node;
return head;
}
8. How Do You Delete a Node from a Linked List?
In a singly linked list, deletion generally requires changing the previous node's
next reference so that it skips the node being removed.
Before:
10 → 20 → 30
Delete 20:
10 → 30
Finding the node can take O(n), but changing the link after the previous node is known takes O(1).
9. How Do You Reverse a Linked List?
This is one of the most frequently asked linked list interview problems. The iterative
approach maintains three references: previous, current, and
next.
function reverseList(head) {
let previous = null;
let current = head;
while (current !== null) {
const next = current.next;
current.next = previous;
previous = current;
current = next;
}
return previous;
}
The algorithm runs in O(n) time and uses O(1) auxiliary space.
10. Can You Reverse a Linked List Recursively?
Yes. The recursive solution reverses the remainder of the list and then connects the current node behind the reversed portion.
function reverseRecursive(head) {
if (head === null || head.next === null) {
return head;
}
const newHead = reverseRecursive(head.next);
head.next.next = head;
head.next = null;
return newHead;
}
The time complexity is O(n), while the recursive call stack requires O(n) space.
11. How Do You Find the Middle of a Linked List?
Use the slow and fast pointer technique. The slow pointer moves one node at a time while the fast pointer moves two nodes at a time.
function findMiddle(head) {
let slow = head;
let fast = head;
while (fast !== null && fast.next !== null) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
When the fast pointer reaches the end, the slow pointer is at the middle. This takes O(n) time and O(1) auxiliary space.
12. How Do You Detect a Cycle in a Linked List?
Floyd's cycle detection algorithm uses a slow pointer and a fast pointer. If the two pointers meet, a cycle exists.
function hasCycle(head) {
let slow = head;
let fast = head;
while (fast !== null && fast.next !== null) {
slow = slow.next;
fast = fast.next.next;
if (slow === fast) {
return true;
}
}
return false;
}
This algorithm runs in O(n) time and uses O(1) auxiliary space.
13. How Do You Find the Starting Node of a Cycle?
After the slow and fast pointers meet, move one pointer back to the head. Then move both pointers one step at a time. The node where they meet is the cycle's starting node.
function detectCycleStart(head) {
let slow = head;
let fast = head;
while (fast !== null && fast.next !== null) {
slow = slow.next;
fast = fast.next.next;
if (slow === fast) {
break;
}
}
if (fast === null || fast.next === null) {
return null;
}
slow = head;
while (slow !== fast) {
slow = slow.next;
fast = fast.next;
}
return slow;
}
14. How Do You Find the Length of a Linked List?
Traverse the list while maintaining a counter.
function getLength(head) {
let count = 0;
let current = head;
while (current !== null) {
count++;
current = current.next;
}
return count;
}
The time complexity is O(n) and the auxiliary space is O(1).
15. How Do You Find the Nth Node from the End?
A common solution uses two pointers. Move the first pointer n positions ahead, then move both pointers together until the first pointer reaches the end.
function nthFromEnd(head, n) {
let fast = head;
let slow = head;
for (let i = 0; i < n; i++) {
if (fast === null) {
return null;
}
fast = fast.next;
}
while (fast !== null) {
fast = fast.next;
slow = slow.next;
}
return slow;
}
This approach takes O(n) time and O(1) auxiliary space without first calculating the complete list length.
16. How Do You Merge Two Sorted Linked Lists?
Compare the current nodes from both lists and repeatedly attach the smaller value to the result. A dummy node can simplify the implementation.
function mergeSortedLists(first, second) {
const dummy = new Node(0);
let current = dummy;
while (first !== null && second !== null) {
if (first.value <= second.value) {
current.next = first;
first = first.next;
} else {
current.next = second;
second = second.next;
}
current = current.next;
}
current.next = first !== null
? first
: second;
return dummy.next;
}
The complexity is O(n + m), where n and m are the lengths of the two lists.
17. Why Is a Dummy Node Useful?
A dummy node provides a fixed starting point for building a linked list. It reduces special-case handling when the result's first node has not yet been determined.
const dummy = new Node(0);
let current = dummy;
// Build result using current.next.
return dummy.next;
Dummy nodes are particularly useful in merge, insertion, deletion, and partition problems.
18. How Do You Check Whether a Linked List Is a Palindrome?
One efficient approach is to find the middle, reverse the second half, and compare both halves.
- Find the middle using slow and fast pointers.
- Reverse the second half.
- Compare values from the first and second halves.
- Optionally restore the second half.
1 → 2 → 3 → 2 → 1
First half:
1 → 2
Second half:
3 → 2 → 1
Compare corresponding values.
An optimized solution can run in O(n) time and O(1) auxiliary space.
19. How Do You Remove Duplicates from a Sorted Linked List?
Because the list is sorted, duplicate values appear next to each other. Compare the current node with the next node and skip the next node when their values match.
function removeDuplicates(head) {
let current = head;
while (current !== null && current.next !== null) {
if (current.value === current.next.value) {
current.next = current.next.next;
} else {
current = current.next;
}
}
return head;
}
20. How Do You Remove Duplicates from an Unsorted Linked List?
A Set can store values that have already appeared. When a duplicate is found, skip that node.
function removeUnsortedDuplicates(head) {
const seen = new Set();
let current = head;
let previous = null;
while (current !== null) {
if (seen.has(current.value)) {
previous.next = current.next;
} else {
seen.add(current.value);
previous = current;
}
current = current.next;
}
return head;
}
This approach takes O(n) time and O(n) additional space.
21. How Do You Find the Intersection of Two Linked Lists?
Two linked lists intersect when they reference the same node object. A two-pointer technique can find the intersection without storing every visited node.
function getIntersectionNode(headA, headB) {
let first = headA;
let second = headB;
while (first !== second) {
first = first === null
? headB
: first.next;
second = second === null
? headA
: second.next;
}
return first;
}
The pointers effectively traverse both list lengths, making the comparison independent of the difference in their individual lengths.
22. What Is the Difference Between Reversing a List and Reversing Links?
In a linked list, reversal normally means changing the direction of the
next references. The node objects themselves do not need to be recreated.
Before:
A → B → C → null
After:
null ← A ← B ← C
New head = C
23. What Is the Time Complexity of Common Linked List Operations?
| Operation | Typical Complexity |
|---|---|
| Access by position | O(n) |
| Search | O(n) |
| Insert at head | O(1) |
| Delete at head | O(1) |
| Insert after known node | O(1) |
| Delete after known previous node | O(1) |
| Traverse entire list | O(n) |
24. What Is the Fast and Slow Pointer Technique?
The fast and slow pointer technique uses two references that move through a linked list at different speeds.
- Slow pointer: usually moves one node at a time.
- Fast pointer: usually moves two nodes at a time.
This pattern is useful for finding the middle node, detecting cycles, locating the cycle entry, and solving several two-pointer problems.
25. What Is the Difference Between Singly and Doubly Linked Lists?
| Feature | Singly Linked List | Doubly Linked List |
|---|---|---|
| References per node | Next | Previous and next |
| Forward traversal | Yes | Yes |
| Backward traversal | No | Yes |
| Memory per node | Lower | Higher |
26. How Do You Reverse a Doubly Linked List?
For a doubly linked list, both the next and prev references
need to be swapped for every node.
function reverseDoublyLinkedList(head) {
let current = head;
let newHead = null;
while (current !== null) {
const next = current.next;
current.next = current.prev;
current.prev = next;
newHead = current;
current = next;
}
return newHead;
}
27. What Is a Circular Linked List?
In a circular linked list, the final node does not point to null.
Instead, it points back to the first node.
10 → 20 → 30
↑ ↓
└───────────┘
Circular lists can be useful for applications where processing repeatedly cycles through a collection.
28. How Can You Detect a Circular Linked List?
Floyd's slow and fast pointer algorithm can detect whether a linked list contains a cycle. If the pointers eventually meet, the list contains a cycle.
29. How Do You Sort a Linked List?
Merge Sort is commonly used for linked lists because the list can be divided and merged without requiring random access.
- Find the middle of the list.
- Split the list into two halves.
- Recursively sort both halves.
- Merge the sorted halves.
Merge Sort runs in O(n log n) time for a linked list.
30. What Are the Most Important Linked List Interview Patterns?
Instead of memorizing every linked list problem separately, learn the patterns behind the problems.
| Pattern | Common Problems |
|---|---|
| Pointer manipulation | Insertion, deletion, reversal |
| Fast and slow pointers | Middle node, cycle detection |
| Two pointers | Nth node from end, intersection |
| Dummy node | Merge, deletion, partition |
| Recursion | Reverse, merge sort, recursive traversal |
| Hashing | Duplicate detection, visited nodes |
31. How Should You Approach a Linked List Interview Problem?
A structured approach can make pointer-based problems easier to reason about.
- Clarify whether the list is singly, doubly, or circular.
- Identify what should happen with an empty list.
- Check whether the head can change.
- Draw a small example before changing any links.
- Identify which pointers need to be preserved before overwriting links.
- Consider whether a dummy node simplifies the implementation.
- Check edge cases such as one node, two nodes, and deletion of the head.
- State the time and space complexity.
Linked List Edge Cases to Check
Pointer problems often fail because an edge case was not considered. Before finalizing a solution, test scenarios such as:
- Empty linked list.
- List containing one node.
- List containing two nodes.
- Deleting the head.
- Deleting the tail.
- Inserting before the head.
- Searching for a value that does not exist.
- Duplicate values.
- Cycle present or absent.
- Lists with different lengths.
Quick Linked List Interview Revision
Access by index → O(n)
Search → O(n)
Insert at head → O(1)
Delete at head → O(1)
Insert after node → O(1)
Delete after node → O(1)
Traverse → O(n)
Reverse → O(n)
Find middle → O(n), O(1) space
Detect cycle → O(n), O(1) space
Nth from end → O(n), O(1) space
Merge sorted lists → O(n + m)
Key Takeaways
- Understand node references before attempting linked list problems.
- Know the difference between singly, doubly, and circular linked lists.
- Practice changing links without losing the rest of the list.
- Learn the fast and slow pointer technique.
- Use dummy nodes when they simplify boundary conditions.
- Be comfortable reversing a linked list iteratively.
- Know how to detect cycles with Floyd's algorithm.
- Understand the time and space complexity of common operations.
- Always test empty, single-node, and small-list cases.
- For interviews, explain your pointer movement before writing the final code.