ProgrammingModerate
In a singly linked list, which operation takes O(1) time when a pointer to the head is given?
- A. Insert a new node after the last node
- B. Insert a new node at the head
- C. Find the length of the list
- D. Remove the last node
Correct answer: B. Insert a new node at the head
Only the head pointer needs to change, so insertion at the head is constant time. The other operations walk the list.
More C Programming questions
- 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?
- Which declaration allocates a 3 x 4 int matrix as one contiguous block with a single free needed?