Data Structures & AlgorithmsModerateWipro NLTH · Linked List
A singly linked list has 10 nodes numbered 1 to 10. Slow and fast pointers both start at node 1; the loop is `while (fast != NULL && fast->next != NULL) { slow = slow->next; fast = fast->next->next; }`. Which node does slow end on?
- A. 5
- B. 7
- C. 4
- D. 6
Correct answer: D. 6
Fast moves 2 nodes per step and runs off the list after 5 iterations, by which time slow has moved 5 nodes, to node 6 (the second middle).
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- What is the maximum number of element comparisons a binary search (one three-way comparison per step) makes on a sorted array of 100…
- The sorted array [1, 1, 2, 2, 2, 3, 5, 5, 8] is de-duplicated in place by two pointers: a write index w starts at 1 and each element a[i]…
- The array [1, 2, 3, 4, 5, 6, 7, 8] is rotated left by 3 positions using three reversals: reverse the first 3 elements, reverse the…
- Keys 12, 22, 32, 13, 23 are inserted in this order into an empty hash table of size 10 using h(k) = k mod 10 and linear probing. At which…
- A queue is built from two stacks: enqueue pushes on stack IN; dequeue pops from OUT, first moving all of IN onto OUT (one pop and one push…
- What is the minimum number of adjacent swaps needed to sort [3, 1, 2, 5, 4] in ascending order?