ProgrammingHard
A linked list is built by appending n nodes one by one, each time walking from the head to the last node. About how many node visits does building a list of N = 10^5 take?
- A. About 10^5
- B. About 10^10
- C. About 5 x 10^9
- D. About 5 x 10^4
Correct answer: C. About 5 x 10^9
The i-th append walks i - 1 nodes, so the total is about N^2 / 2 = 5 x 10^9. A tail pointer makes every append O(1).
More C Programming questions
- A stack is implemented with a singly linked list, pushing at the head. The operations are push 1, push 2, pop, push 3, push 4, pop, pop…
- A queue is kept as a singly linked list with head and tail pointers. Enqueue 1, 2, 3, dequeue twice, enqueue 4, then dequeue once. Which…
- Which loop correctly frees every node of a singly linked list whose head is p?
- A recursive function prints a list after its recursive call returns, as in print(n) { if (!n) return; print(n->next); putchar(n->v + '0')…
- In a 6-node list with values 10, 20, 30, 40, 50, 60, the standard slow and fast pointer method (slow moves one step, fast two steps, while…
- Which operation removes the k-th node (1-based) from a singly linked list in one pass with a pointer to the previous node, and handles…