Linked List Interview Problems — Complete Guide with Solutions
In this tutorial, you'll learn about Linked List Interview Problems. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
Linked List problems test your ability to manipulate pointers and handle edge cases in a dynamic data structure. Unlike arrays, linked lists require careful pointer management and are a favorite topic for assessing coding fundamentals.
Learning Path
flowchart LR A["Array & String Problems"] --> B["Linked List Problems
You are here"] B --> C["Tree & Graph Problems"] C --> D["System Design Prep"] style B fill:#f90,color:#fff,stroke-width:2px
Reverse a Linked List
Reversal is the most fundamental Linked List operation and appears in many variations.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def reverse_list(head):
prev = None
curr = head
while curr:
next_temp = curr.next
curr.next = prev
prev = curr
curr = next_temp
return prev
def list_to_array(head):
result = []
while head:
result.append(head.val)
head = head.next
return result
head = ListNode(1, ListNode(2, ListNode(3, ListNode(4, ListNode(5)))))
print(list_to_array(reverse_list(head)))
[5, 4, 3, 2, 1]
class ListNode {
int val;
ListNode next;
ListNode(int val) { this.val = val; }
}
public class ReverseList {
public static ListNode reverseList(ListNode head) {
ListNode prev = null, curr = head;
while (curr != null) {
ListNode next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
return prev;
}
public static void main(String[] args) {
ListNode head = new ListNode(1);
head.next = new ListNode(2);
head.next.next = new ListNode(3);
ListNode reversed = reverseList(head);
while (reversed != null) {
System.out.print(reversed.val + " ");
reversed = reversed.next;
}
}
}
3 2 1
struct ListNode {
int val;
ListNode* next;
ListNode(int x) : val(x), next(nullptr) {}
};
ListNode* reverseList(ListNode* head) {
ListNode* prev = nullptr;
ListNode* curr = head;
while (curr) {
ListNode* next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
return prev;
}
Cycle Detection (Floyd's Algorithm)
Detect a cycle in a Linked List using slow and fast pointers meeting in O(n) time with O(1) space.
def has_cycle(head):
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
return True
return False
def detect_cycle_start(head):
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
slow = head
while slow != fast:
slow = slow.next
fast = fast.next
return slow
return None
# Create a linked list with a cycle
nodes = [ListNode(i) for i in range(6)]
for i in range(5):
nodes[i].next = nodes[i + 1]
nodes[5].next = nodes[2] # create cycle at node with value 2
print(has_cycle(nodes[0]))
print(detect_cycle_start(nodes[0]).val if detect_cycle_start(nodes[0]) else None)
True
2
public class CycleDetection {
public static boolean hasCycle(ListNode head) {
ListNode slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) return true;
}
return false;
}
public static ListNode detectCycle(ListNode head) {
ListNode slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) {
slow = head;
while (slow != fast) {
slow = slow.next;
fast = fast.next;
}
return slow;
}
}
return null;
}
}
Merge Two Sorted Lists
Merging sorted linked lists is a common interview problem that tests your ability to traverse two lists simultaneously.
def merge_two_lists(l1, l2):
dummy = ListNode(0)
curr = dummy
while l1 and l2:
if l1.val <= l2.val:
curr.next = l1
l1 = l1.next
else:
curr.next = l2
l2 = l2.next
curr = curr.next
curr.next = l1 or l2
return dummy.next
l1 = ListNode(1, ListNode(3, ListNode(5)))
l2 = ListNode(2, ListNode(4, ListNode(6)))
merged = merge_two_lists(l1, l2)
print(list_to_array(merged))
[1, 2, 3, 4, 5, 6]
Common Mistakes
- Losing reference to the next node — Always save
curr.nextbefore reassigningcurr.nextin reversal operations. - Forgetting the dummy node — A dummy head simplifies merge and insertion operations by eliminating the empty-list edge case.
- Infinite loops from cycles — Always check
fast and fast.nextbefore advancing the fast pointer in cycle detection. - Not handling null or single-node lists — Every operation must handle empty lists and single-element lists as separate cases.
- Mutual Recursion infinite loop — When two Linked List nodes point to each other, traversal without a visited set causes infinite loops.
- Confusing value equality with reference equality — Use
is(Python) or==(Java/C++) appropriately when comparing nodes. - Incorrect mid-point calculation — When finding the middle element, initialize
slow = headandfast = headand advancefast = fast.next.next.
Practice Questions
1. Find the middle node of a Linked List in one pass.
Use slow and fast pointers. When fast reaches the end, slow is at the middle. For even-length lists, return the second middle node.
2. Remove the N-th node from the end of a Linked List.
Use Two Pointers with a gap of N nodes. When the leading pointer reaches the end, the trailing pointer is at the node to remove.
3. Challenge: Reorder List (Leetcode 143)
Given L0 -> L1 -> ... -> Ln-1 -> Ln, reorder it to L0 -> Ln -> L1 -> Ln-1 -> L2 -> Ln-2 -> .... Find the middle, reverse the second half, then merge.
FAQ
Related Tutorials
Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro. Updated 2026-06-23.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro