Question 21
EasyWhat is the time complexity of finding the length of a singly linked list (with no stored count)?
- A.
O(1)
- B.
O(log n)
- C.
O(n)
- D.
O(n²)
Sign up free to see the answer and a step-by-step explanation.
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Data Structures and Algorithms
What is the time complexity of finding the length of a singly linked list (with no stored count)?
O(1)
O(log n)
O(n)
O(n²)
Sign up free to see the answer and a step-by-step explanation.
Insertion and removal at the head of a singly linked list are both O(1). Which data structure can it implement directly?
A stack
A hash table
A heap
A binary search tree
Sign up free to see the answer and a step-by-step explanation.
What kind of structure is a Python list?
A hash table
A doubly linked list
A singly linked list
A dynamic array
Sign up free to see the answer and a step-by-step explanation.
What kind of list is Java's LinkedList class?
A doubly linked list
A singly linked list
A circular array
A dynamic array
Sign up free to see the answer and a step-by-step explanation.
In a singly linked list, how do you delete the node after node p?
Set p.next = p.next.next
Set p.next = p
Set p = null
Swap p with the head
Sign up free to see the answer and a step-by-step explanation.
To insert newNode after node p in a singly linked list, which order of steps is correct?
p = newNode, then newNode.next = p
p.next = newNode, then newNode.next = p.next
newNode.next = p, then p.next = null
newNode.next = p.next, then p.next = newNode
Sign up free to see the answer and a step-by-step explanation.
Where is the memory for a linked list's nodes usually allocated?
In one contiguous block
On disk
In CPU registers
Dynamically on the heap, one node at a time
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
class Node:
def __init__(self, v, nxt=None):
self.v, self.next = v, nxt
head = Node(1, Node(2, Node(3)))
prev = None
while head:
head.next, prev, head = prev, head, head.next
out = []
while prev:
out.append(str(prev.v)); prev = prev.next
print(" ".join(out))
1 3 2
3 1 2
3 2 1
1 2 3
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
class Node:
def __init__(self, v, nxt=None):
self.v, self.next = v, nxt
head = Node(1, Node(2, Node(3, Node(4, Node(5)))))
slow = fast = head
while fast and fast.next:
slow, fast = slow.next, fast.next.next
print(slow.v)
4
5
2
3
Sign up free to see the answer and a step-by-step explanation.
How does Floyd's algorithm detect a cycle in a linked list?
A fast and a slow pointer will meet if and only if there is a cycle
By reversing the list
By counting the nodes
By marking every node as visited in a hash set
Sign up free to see the answer and a step-by-step explanation.