ProgrammingVery hard
In a list 1 -> 2 -> 3 -> 4 -> 2 where the last node points back to the node holding 2, Floyd's method runs slow = slow->next and fast = fast->next->next from the head, and stops when they meet. How many iterations run?
- A. 2
- B. 3
- C. 4
- D. 5
Correct answer: B. 3
After iteration 1 slow is at 2 and fast at 3. After iteration 2 slow is at 3 and fast at 2. After iteration 3 both are at 4, so they meet after 3 steps.
More C Programming questions
- A pointer-to-pointer loop deletes every node holding 2 from 2 -> 2 -> 3 -> 2 -> 1, using struct N pp = &head and advancing only when no…
- Which code correctly removes node p from a doubly linked list, where p->prev and p->next may be NULL, and then frees it?
- Merging two sorted singly linked lists of length 3 each with the standard merge, what is the largest number of key comparisons needed?
- With a 4-byte int and an 8-byte pointer, natural alignment, what is sizeof(struct N) for struct N { int v; struct N next; };?
- A linked list is built by appending n nodes one by one, each time walking from the head to the last node. About how many node visits does…
- A stack is implemented with a singly linked list, pushing at the head. The operations are push 1, push 2, pop, push 3, push 4, pop, pop…