Linked Lists Questions with Answers

66 practice questions on Linked Lists (Data Structures & Algorithms) with answers and step-by-step solutions. Try 10 samples below, then practise all 66 free in the AptiNest app.

Sample questions

Question 1Easy

In a singly linked list with only a head pointer, which operation is O(1)?

  1. Deleting the last node
  2. Inserting at the head
  3. Inserting at the tail
  4. Finding the middle node

Answer: B. Inserting at the head

Explanation

Only the head is directly reachable; anything at the end needs a full traversal.

Step-by-step solution

  1. With only head, the first node is reached in one step.
  2. Inserting at the head: new.next = head; head = new. Constant work.
  3. Tail operations and the middle need walking the list: O(n).

Why the other options are wrong

  • A. You must find the second-last node, which takes O(n).
  • C. Without a tail pointer you must walk to the end first.
  • D. Finding the middle needs a traversal (e.g. slow/fast pointers).

Tip: Singly list with head only: O(1) at the front, O(n) everywhere else.

Question 2Easy

Given the Node class and helpers build (creates a linked list from a Python list) and show (prints its values as a list), what does this code print?

def count_circular(head):
    if head is None:
        return 0
    n, cur = 1, head.next
    while cur is not head:
        n, cur = n + 1, cur.next
    return n

h = build([5, 6, 7, 8, 9])
t = h
while t.next:
    t = t.next
t.next = h
print(count_circular(h))
  1. 5
  2. 10
  3. 4
  4. 6

Answer: A. 5

Explanation

The list is made circular by linking the tail to the head; counting until the head reappears gives 5 nodes.

Step-by-step solution

  1. The tail 9 is linked back to the head 5, making a circle of 5 nodes.
  2. Start n = 1 at the head and move to the next node.
  3. Count until the head appears again: 6, 7, 8, 9 add 4 more.
  4. n = 5.

Why the other options are wrong

  • B. The loop stops when it returns to the head; it does not go around twice.
  • C. The head itself is counted (n starts at 1).
  • D. There are only 5 nodes; no extra is added.

Tip: In a circular list, stop on 'cur is head', not on None.

Question 3Easy

What is the time complexity of inserting a node at the head of a singly linked list?

  1. O(n)
  2. O(log n)
  3. O(1)
  4. O(n^2)

Answer: C. O(1)

Explanation

Create the node, point its next to the current head, and update head. No traversal is needed.

Step-by-step solution

  1. Create the new node.
  2. Set new.next = head.
  3. Set head = new. No traversal, so O(1).

Why the other options are wrong

  • A. O(n) is inserting at the tail without a tail pointer.
  • B. No halving is involved.
  • D. Nothing here is quadratic.

Tip: Insert at head: O(1). Insert at tail without a tail pointer: O(n).

Question 4Easy

In a circular singly linked list, how do you know a traversal from the head has completed?

  1. The previous pointer is null
  2. The next pointer is null
  3. The next node is the head again
  4. A node's value is 0

Answer: C. The next node is the head again

Explanation

There is no null in a circular list; the walk stops when it returns to the start.

Step-by-step solution

  1. The last node of a circular list points back to the head.
  2. No next pointer is ever null.
  3. Stop when the next node is the head.

Why the other options are wrong

  • A. Singly linked nodes have no previous pointer.
  • B. In a circular list no next pointer is null.
  • D. Values have nothing to do with structure.

Tip: Circular list: loop until you are back at head, never until null.

Question 5Medium

What does this function return for a list with nodes 4 -> 8 -> 15 -> 16?

def f(head):
    if head is None or head.next is None:
        return head
    return f(head.next)
  1. The node with value 4
  2. The node with value 16
  3. The node with value 15
  4. None

Answer: B. The node with value 16

Explanation

f recurses until it reaches a node with no successor, which is the last node.

Step-by-step solution

  1. f(4): next exists, recurse on 8.
  2. f(8) -> f(15) -> f(16).
  3. 16.next is None, so f returns node 16, and each call passes it back up.

Why the other options are wrong

  • A. The head is only returned when it has no successor.
  • C. 15 still has a next, so recursion continues.
  • D. None is returned only for an empty list.

Tip: Base case 'head.next is None' means 'I am the last node'.

Question 6Medium

In tree construction, which is the suitable and efficient data structure?

  1. Array
  2. Linked list
  3. Stack
  4. Graph

Answer: B. Linked list

Explanation

If a linked list is used then a node can hold the address of both the left child and right child. Stack, Queue and Graph can never be used. Arrays can also be used but careful indexing is required to be done and hence is not a valid data structure.

Step-by-step solution

  1. A tree needs each node to hold links to its children.
  2. A linked list lets each node hold the addresses of its child nodes directly.
  3. Arrays can be used but need careful index calculation, and stacks, queues and graphs do not fit tree building directly.
  4. So the correct answer is option B, Linked list.

Why the other options are wrong

  • A. An array can be used, but it needs careful indexing, so it is less suitable.
  • C. A stack is for last-in first-out order, not for storing tree links.
  • D. A graph is more general than a tree and is not the structure used for building one.

Tip: Tree nodes with child pointers fit a linked structure best.

Question 7Medium

An LRU cache with O(1) get and put is usually built from:

  1. a hash map plus a singly linked list
  2. a sorted array
  3. a binary heap
  4. a hash map plus a doubly linked list

Answer: D. a hash map plus a doubly linked list

Explanation

The map finds the node in O(1), and the doubly linked list moves it to the front or evicts the tail in O(1).

Step-by-step solution

  1. get needs O(1) lookup: a hash map from key to node.
  2. Recency updates need O(1) removal from the middle and insertion at the front.
  3. A doubly linked list gives that, so map + doubly linked list.

Why the other options are wrong

  • A. A singly linked list cannot remove a middle node in O(1) without its predecessor.
  • B. Sorted arrays need O(n) shifts.
  • C. A heap gives O(log n), not O(1).

Tip: LRU = HashMap + Doubly Linked List (Java's LinkedHashMap does this).

Question 8Medium

The linked list 2 -> 4 -> 6 -> 8 -> 10 -> 12 -> 14 -> 16 -> 18 -> 20 is reversed in groups of 4 nodes; a final group with fewer than 4 nodes is left as it is. What is the result?

  1. 20 -> 18 -> 16 -> 14 -> 12 -> 10 -> 8 -> 6 -> 4 -> 2
  2. 8 -> 6 -> 4 -> 2 -> 16 -> 14 -> 12 -> 10 -> 18 -> 20
  3. 10 -> 12 -> 14 -> 16 -> 18 -> 20 -> 2 -> 4 -> 6 -> 8
  4. 8 -> 6 -> 4 -> 2 -> 16 -> 14 -> 12 -> 10 -> 20 -> 18

Answer: B. 8 -> 6 -> 4 -> 2 -> 16 -> 14 -> 12 -> 10 -> 18 -> 20

Explanation

Each full block of 4 is reversed in place and relinked to the next block: 8 -> 6 -> 4 -> 2 -> 16 -> 14 -> 12 -> 10 -> 18 -> 20.

Step-by-step solution

  1. Groups of 4: [2, 4, 6, 8], [10, 12, 14, 16], [18, 20].
  2. Reverse full groups: 8 6 4 2 and 16 14 12 10.
  3. [18, 20] stays as it is.
  4. Result 8 6 4 2 16 14 12 10 18 20.

Why the other options are wrong

  • A. This reverses the whole list.
  • C. This rotates the list.
  • D. The last group has only 2 nodes and must not be reversed.

Tip: Check that k nodes remain before reversing a group.

Question 9Hard

Given only a pointer to a node in a singly linked list (not the head, and not the last node), how can that node be deleted in O(1)?

  1. Set the node pointer to NULL
  2. Copy the next node's data into it, then unlink the next node
  3. It is impossible without the head pointer
  4. Traverse from the node back to the previous node

Answer: B. Copy the next node's data into it, then unlink the next node

Explanation

Since we cannot reach the previous node, we overwrite the current node with its successor and delete the successor. This fails for the last node.

Step-by-step solution

  1. We cannot reach the previous node to change its next pointer.
  2. Copy the next node's value into the current node.
  3. Set current.next = next.next, removing the next node.
  4. The list now looks as if the given node was deleted.

Why the other options are wrong

  • A. Changing a local pointer does not change the list.
  • C. The copy trick makes it possible.
  • D. A singly linked list has no back links.

Tip: Cannot delete yourself: become your successor, then delete the successor.

Question 10Hard

A linked list has nodes with values 1, 2, 3, 4, 5, 6, 7, 8 in order, and the last node's next points back to the node with value 5. Floyd's algorithm starts slow and fast at the head (value 1); each step moves slow 1 node and fast 2 nodes. At which node value do they first meet?

  1. 6
  2. 8
  3. 5
  4. 4

Answer: C. 5

Explanation

Simulating both pointers, they first land on the same node after 4 steps, at value 5. Resetting one pointer to the head and moving both one step at a time would then meet at the cycle start, 5.

Step-by-step solution

  1. Start: slow 1, fast 1.
  2. Step 1: slow 2, fast 3. Step 2: slow 3, fast 5.
  3. Step 3: slow 4, fast 7. Step 4: slow 5, fast 5 (8 -> 5).
  4. They meet at 5.

Why the other options are wrong

  • A. Neither pointer is at 6 when they meet.
  • B. fast passes through 8 but slow is not there.
  • D. At node 4 they are apart.

Tip: Simulate step by step; wrap from the last node to the cycle start.

Practise all 66 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Data Structures & Algorithms topics · All topics