سوال ۶۷ کنکور ارشد علوم کامپیوتر ۱۳۹۸
فرض کنید ( x = (x_1, x_2, ..., x_n و ( y = (y_1, y_2, ..., y_m دو لیست پیوندی خطی ساده باشند. مرتبه زمانی الگوریتمی که دو لیست را در لیست z ترکیب میکند، کدام است؟
پاسخ صحیح: گزینهٔ D —
What the algorithm builds
List is formed by alternately interleaving nodes of and — — and the alternation continues only as far as the shorter list allows. Let . After interleaved pairs, one list is exhausted and the remaining suffix of the longer list is appended.
Why it is , not
The lists are singly linked, so combining them is done by pointer splicing, not by copying values.
-
Interleaving phase. To weave we rewire the
nextpointers of the first nodes. Each redirection is , and there are of them, so this phase costs . -
Append phase. When the shorter list runs out, the rest of the longer list (the nodes or ) is already a correctly ordered chain. We do not need to walk it — at the point of exhaustion we are sitting on the node whose
nextpointer still points to that suffix's head, so the suffix stays attached automatically (or is spliced with a single assignment). This is .
The loop stops the moment the shorter list ends, so the number of iterations is governed by :
Eliminating the distractors
| Option | Verdict |
|---|---|
| No nested traversal occurs; nothing multiplies the two sizes. | |
| This is the trap: it would be the cost if you copied/visited every element (as in arrays). With splicing, the longer tail is attached in and never traversed. | |
| The longer list's surplus is not iterated, so does not appear. | |
| Correct — the controlling loop runs times. |
Exam trick: On linked lists, "append the rest of the longer list" is an splice — the work is bounded by where the alternation stops, i.e. . Reflexively answering is the array-thinking mistake.