Question 51
MediumWhich sorting algorithm is best suited to linked lists?
- A.
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Data Structures and Algorithms
Which sorting algorithm is best suited to linked lists?
Merge sort
Heap sort
Quick sort with random access
Binary insertion sort with indexes
Sign up free to see the answer and a step-by-step explanation.
How do you find the length of a loop in a linked list after Floyd's pointers meet?
It can't be found
Count all nodes from the head
Reverse the list
Keep one pointer still and move the other around until they meet again, counting steps
Sign up free to see the answer and a step-by-step explanation.
What happens in a singly linked list if you lose the head pointer without saving it?
Nothing
The whole list becomes unreachable (a memory leak in languages without GC)
Only the head is lost
The list becomes circular
Sign up free to see the answer and a step-by-step explanation.
Which data structure is often built from a linked list to get O(1) push and pop?
A heap
A binary search tree
A hash table
A stack
Sign up free to see the answer and a step-by-step explanation.
After the slow and fast pointers meet inside a cycle, how is the start of the cycle found?
Reset one pointer to the head and move both one step at a time; they meet at the cycle's start
Count the cycle length and divide by two
The meeting point is always the start
Reverse the list
Sign up free to see the answer and a step-by-step explanation.
How can you check whether a singly linked list is a palindrome in O(n) time and O(1) extra space?
Find the middle, reverse the second half in place, then compare the two halves
Push every node onto a stack
Sort the list first
Copy it into an array and compare from both ends
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
class N:
def __init__(self, v, nxt=None):
self.v, self.next = v, nxt
head = N(1, N(2, N(3, N(4, N(5)))))
fast = slow = head
for _ in range(2):
fast = fast.next
while fast.next:
fast, slow = fast.next, slow.next
slow.next = slow.next.next
out = []
while head:
out.append(str(head.v)); head = head.next
print(" ".join(out))
1 2 4 5
1 2 3 5
Sign up free to see the answer and a step-by-step explanation.
Two singly linked lists merge at some node. How can you find the intersection node in O(m + n) time and O(1) space?
Reverse both lists permanently
Compare every pair of nodes
Walk two pointers; when one reaches the end, restart it at the other list's head — they meet at the intersection
Sort both lists
Sign up free to see the answer and a step-by-step explanation.
What is the time complexity of sorting a linked list with merge sort?
O(n log n) time and O(n) extra arrays
O(n)
O(n²)
O(n log n) time and O(log n) extra space for recursion
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
class N:
def __init__(self, v):
self.v, self.next = v, None
a, b, c, d = N(1), N(2), N(3), N(4)
a.next, b.next, c.next, d.next = b, c, d, b
slow = fast = a
cycle = False
while fast and fast.next:
slow, fast = slow.next, fast.next.next
if slow is fast:
cycle = True
break
print(cycle)
True
Error
Sign up free to see the answer and a step-by-step explanation.
1 3 4 5
2 3 4 5
False
None