ProgrammingModerate
Deleting a node from the middle of a doubly linked list of length n costs what, if a pointer to the node is already known?
- A. O(n), because the head must be found first
- B. O(log n), because the list is balanced
- C. O(1), because the node's prev and next pointers give the neighbours directly
- D. O(n^2), because every node is shifted
Correct answer: C. O(1), because the node's prev and next pointers give the neighbours directly
With a known node, its neighbours are reached through prev and next. No traversal is needed.
More C Programming questions
- A chained table with 10 buckets holds 30 keys. What is the average chain length?
- A dynamic array grows from capacity 1 by doubling. How many doublings are needed to reach capacity 1024 from 1?
- A list of 6 nodes is split into two halves by a slow and fast pointer. The slow pointer is at the start of the second half. Which node…
- A stack is kept as a linked list with the top at the head. Push A, push B, pop, push C, pop. Which element is on top at the end?
- A queue made from a singly linked list with a head and a tail pointer enqueues 1, 2 and 3, then dequeues once. What is the front value now?
- A binary search tree is built by inserting 5, 3, 8, 1 in that order. How many nodes are in the left subtree of the root?