UGC-NET Dec 2018 · Divide & Conquer: Mergesort

MCQ · 2 marks · Problem PROG-DIVC-0017

Consider a singly linked list. What is the worst case time complexity of the best-known algorithm to delete the node $a$, pointer to this node is $q$, from the list?
  1. A
    $O(n \lg \: n)$
  2. B
    $O(n)$
  3. C
    $O(\lg \: n)$
  4. D
    $O(1)$

Solve it on GATE DA practice →More Searching & Sorting Algorithms problems

Show the worked solutionTo unlink node \(q\) we must change the next pointer of its predecessor, and a singly linked list has no back pointers. If \(q\) is not the last node, a trick works in \(O(1)\): copy the data of \(q\to\text{next}\) into \(q\) and delete the next node instead. If \(q\) is the last node there is no successor to copy from, so the predecessor has to be found by walking from the head, which takes \(O(n)\). The worst case is therefore \(O(n)\).Answer: B

GATE DA practice: past exam problems by chapter, with worked solutions · gatedaprep.com