KonkurStudy
همه کارت‌ها

Linked Lists: Search, Insert, Delete Costs

A linked list trades the array's O(1)O(1) random access for O(1)O(1) insert/delete at a known node, but pays Θ(n)\Theta(n) to search — so binary search never applies, even on a sorted list.

کارت مفهوم کنکور ارشد علوم کامپیوتر

اهمیت
۰/۵

Definition and intuition

A linked list stores elements in nodes scattered in memory; each node holds a value plus pointer(s) to its neighbour(s). There is no index arithmetic, so the ii-th element is reachable only by walking from an endpoint — the defining difference from an array.

  • Singly linked list: each node has one pointer, next. You can traverse forward only.
  • Doubly linked list: each node has next and prev. You can traverse both directions and splice a node out using only a pointer to that node.

The key cost facts

The rule that decides every konkur question here:

A linked list has no random access. Reaching position ii costs Θ(i)\Theta(i), so reaching an arbitrary element is Θ(n)\Theta(n).

This breaks binary search, which needs O(1)O(1) indexing plus sorted order. A linked list can be sorted but never indexed in O(1)O(1), so search stays sequential.

Operation Linked list Array
Access element by index ii Θ(i)\Theta(i) (walk) Θ(1)\Theta(1)
Search (unsorted) Θ(n)\Theta(n) Θ(n)\Theta(n)
Search (sorted) Θ(n)\Theta(n) — no binary search Θ(log⁡n)\Theta(\log n)
Insert / delete at a known node Θ(1)\Theta(1) (relink pointers) Θ(n)\Theta(n) (shift elements)
Insert / delete at a searched-for position Θ(n)\Theta(n) (find) + Θ(1)+\,\Theta(1) Θ(n)\Theta(n)

The one place a list wins cleanly: once you already hold a pointer to the spot, splicing in or out is Θ(1)\Theta(1), because you only rewire a constant number of pointers and never shift the rest. (Deleting a node given only its own pointer needs the doubly linked version, or the copy-from-next trick on a singly linked one.)

How to apply it

For any "order of search" question, ask two things in sequence:

  1. Can I jump to a candidate in O(1)O(1)? Only if the container is an array. If yes and it is sorted, binary search gives Θ(log⁡n)\Theta(\log n).
  2. Otherwise I must traverse, paying Θ(n)\Theta(n) — and if a structure mixes both (e.g. an array of lists), sum the cost of each stage and keep the dominant term.

Mini-example (konkur 1402, Q68)

An array of nn cells, each cell a singly-linked sorted list of nn numbers, with every cell's values below the next cell's. To find a target among the n2n^{2} values:

  • Stage 1 — pick the cell. Each list's head is its minimum, reachable in O(1)O(1), and heads increase across the array, so binary-search the cells: O(log⁡n)O(\log n).
  • Stage 2 — search inside the cell. It is a singly-linked list: no random access, so scan it linearly: O(n)O(n).
T=O(log⁡n)+O(n)=Θ(n).T = O(\log n) + O(n) = \Theta(n).

The linear walk inside the list dominates the binary search over cells.

Pitfall: "Sorted" tempts you toward Θ(log⁡n)\Theta(\log n), but binary search needs O(1)O(1) random access. A sorted linked list has the order yet lacks the indexing, so its search is stuck at Θ(n)\Theta(n) — and any structure that must traverse a list inherits that Θ(n)\Theta(n) floor.

فرمول‌های کلیدی

Linked-list search (sorted or not)
Tsearch=Θ(n)T_{\text{search}} = \Theta(n)

Sequential scan; sorting cannot speed it up because the list offers no constant-time random access.

Splice at a known node
Tinsert=Tdelete=Θ(1)T_{\text{insert}} = T_{\text{delete}} = \Theta(1)

Inserting or deleting given a pointer to the node only rewires a constant number of pointers — no element shifting.

Access $i$-th element
Taccess(i)=Θ(i)T_{\text{access}}(i) = \Theta(i)

Reaching position $i$ requires walking $i$ links from an endpoint; an array does this in $\Theta(1)$.

#linked-list#singly-linked-list#doubly-linked-list#random-access#search#binary-search#insert-delete#time-complexity#array-vs-list

سوالات تمرینی