ProgrammingModerate
A list reversal is done in place, and then the list is traversed to print it. What is the time for the reversal alone on n nodes?
- A. O(n^2)
- B. O(n)
- C. O(log n)
- D. O(1)
Correct answer: B. O(n)
In-place reversal walks the list once with prev, curr and next pointers, re-pointing each link a single time: O(n) time and O(1) extra space. O(1) is the space bound, not the time.
More C Programming questions
- A singly linked list of 2 x 10^5 nodes receives 2 x 10^5 insertions at the front and 2 x 10^5 deletions from the front. What is the total…
- A doubly linked list deletes its head by setting new head = head->next and then new head->prev = NULL. What happens if the list had only…
- printf("%d", (5 & 3) << 2 | 1); What is printed?
- int x = 16; printf("%d", x >> 2 ^ 3); What is printed?
- unsigned v = 12; printf("%u", v & -v); What is printed?
- printf("%u", (0u - 1u) % 10); What is printed with 32-bit unsigned int?