Arrays & Linked Lists


Answer & solution
Official answer: C
Worked explanation
The pointer rewiring for insertion is constant time once the predecessor is known. The expensive part is finding that predecessor. For the ith insertion a singly or doubly linked list can require Θ(i) node visits, since it has no random-access midpoint for binary search. Summing i from 0 to n−1 gives n(n−1)/2, which is Θ(n²). A tail pointer can make some input orders cheap but not all possible insertion positions; the question asks for worst case. Sorting the complete input first changes the online-maintenance task and is not the ordinary operation being analyzed.
Derivation and answer
Maintaining the list in sorted order after each online insertion can require scanning all current nodes to locate the insertion point. Summing worst-case costs 0+1+…+(n−1) givesΘ(n²), C. This assumes the ordinary linked-list representation without an auxiliary search index or presorting all future inputs.