ProgrammingModerate
Which kind of list makes it O(1) to delete a node given only a pointer to that node?
- A. A doubly linked list, using the prev and next pointers of the node
- B. A singly linked list, because only next is needed
- C. A plain array, because elements are contiguous
- D. A circular buffer without links
Correct answer: A. A doubly linked list, using the prev and next pointers of the node
With prev the node can unlink itself in constant time. In a singly linked list the predecessor must be found by walking from the head.
More C Programming questions
- In a singly linked list, which operation takes O(1) time when a pointer to the head is given?
- Two pointers walk a linked list: fast goes two nodes per step, slow goes one. The list has an even number of nodes N = 8 and no cycle…
- A cyclic list has a head node whose next points back to itself. What does a naive while (p) loop starting from head do?
- In C, what is the value of a freshly malloc'd int that the program never initialises?
- A linked list is reversed with the recursive function rev(n): if (!n || !n->next) return n; h = rev(n->next); n->next->next = n; n->next =…
- A malloc'd array of 10 structs is later passed to free(&arr[3]). What is the correct description?