Data Structures & AlgorithmsModerateGATE CS · DSA
Two ways of finding the middle of a linked list with slow/fast pointers. What is printed?
Two ways of finding the middle of a linked list with slow/fast pointers. What is printed?
h = build([25, 47, 35, 52, 83, 28, 17, 95]) s = f = h while f and f.nx: s, f = s.nx, f.nx.nx s2 = f2 = h while f2.nx and f2.nx.nx: s2, f2 = s2.nx, f2.nx.nx print(s.v, s2.v)
- A. 83 52
- B. 83 51
- C. 83 53
- D. 52 83
Correct answer: A. 83 52
For an even length, the first loop stops on the SECOND middle and the second loop on the FIRST middle; for an odd length both find the same node. Output: 83 52.
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
- Evaluate the postfix expression 3 5 4 6 1 ÷ − + + (all divisions are exact). What is its value, and what is the maximum number of operands…
- Count palindromic substrings by expanding around all 2n − 1 centres, and also count DISTINCT palindromic substrings. What is printed?…
- Top-down merge sort (split at mid = n // 2, recurse, then merge, stopping comparisons when one side runs out) sorts [33, 53, 13, 42, 11…
- Assertion (A): Removing duplicates from a sorted array can be done in place in O(n) time with two pointers. Reason (R): In a sorted array…
- Assertion (A): Quick sort with the last element as pivot takes Θ(n²) time on an array that is already sorted. Reason (R): On sorted input…
- A Python 2-D list is created two ways and one cell is set. What is printed? python a = [[0] 3] 3 b = [[0] 3 for in range(3)] a[2][2] = 5…