Data Structures & AlgorithmsModerateProduct-company MCQ pattern
In a singly linked list you are given only a pointer to a node X (not the last node) and no head pointer. How can X be deleted, and in what time?
- A. Copy the next node's data into X and unlink the next node: O(1)
- B. It cannot be deleted without the head pointer
- C. Traverse from the head to X's predecessor: O(n)
- D. Swap X with the tail node: O(n)
Correct answer: A. Copy the next node's data into X and unlink the next node: O(1)
X takes over its successor's data and next pointer, and the successor is freed, which is O(1). The trick fails only for the last node, which is why the question excludes it.
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
- Which of these is NOT a typical application of a stack?
- A sorted array of n integers is given. Using the two-pointer technique, what are the time and extra space needed to decide whether some…
- The numbers 1, 2, 3, 4, 5 are pushed onto a stack in that order, and pops may happen at any time in between. Which output sequence is…
- Keys 15, 11, 27, 8, 12, 22, 29, 1 are inserted into a hash table of size 7 with h(k) = k mod 7 and separate chaining. What is the length…
- Keys 22, 33 and 44 are inserted in that order into an empty table of size 11 with h(k) = k mod 11 and quadratic probing hᵢ(k) = (h(k) +…
- Two sorted lists of 5 and 7 elements are merged by the standard merge procedure. What is the maximum number of comparisons it can make?