ProgrammingHard
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. After the loop while (fast && fast->next), how many steps has slow taken?
- A. 3
- B. 5
- C. 4
- D. 8
Correct answer: C. 4
Fast reaches the last node after 4 steps when N = 8. Slow has advanced one per step, so slow took 4 steps as well.
More C Programming questions
- 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?
- A list of N nodes is freed with free() on each node but the next pointer is read after freeing it. Which order is correct?