Data Structures & AlgorithmsModerateTCS Digital · Infosys SP
Delete every alternate node (2nd, 4th, …) of a linked list, then reverse what remains. What is printed?
Delete every alternate node (2nd, 4th, …) of a linked list, then reverse what remains. What is printed?
def del_alt(h):
p = h
while p and p.nx:
p.nx = p.nx.nx
p = p.nx
return h
def rev(h):
prev = None
while h: h.nx, prev, h = prev, h, h.nx
return prev
print(vals(rev(del_alt(build([59, 53, 47, 55, 38, 1, 35, 29, 17])))))- A. [17, 38, 35, 47, 59]
- B. [59]
- C. [17, 35, 38, 47, 59]
- D. [17, 35, 38, 59, 47]
Correct answer: C. [17, 35, 38, 47, 59]
After each deletion p jumps to the node that followed the deleted one, so positions 1, 3, 5, … survive; reversing them gives [17, 35, 38, 47, 59].
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
- How many contiguous subarrays of [5, 3, 4, 5, 1, 1, 6, 5] have a sum between 6 and 10 inclusive? (All values positive: count atMost(10) −…
- Keys 80, 42, 55, 92, 96, 24, 95, 69 are inserted in this order into an empty hash table of size 11 with h(k) = k mod 11 and linear…
- Two pointers start at both ends of the sorted array [2, 5, 10, 11, 12, 14, 16, 18, 21, 29]. How many pairs (i < j) have a[i] + a[j] ≤ 28?…
- Selection sort (ascending; in pass i find the minimum of the unsorted part and swap it into position i only if it is not already there) is…
- Rotate a linked list to the RIGHT by k places (k may exceed the length). What is printed? python def rotate(h, k): n, t = 1, h while t.nx…
- Exponential search: double the bound i (1, 2, 4, …) until a[i] ≥ key or i passes the end, then binary-search the last range. How many…