What are the time complexities of finding 8th element from beginning and 8th…
2024
What are the time complexities of finding 8th element from beginning and 8th element from end in a singly linked list? Let n be the number of nodes in linked list, you may assume that n > 8.
Answer: A. O(1) and O(n) — Concept: Big-O measures how the number of steps an algorithm takes grows as the input size n grows. If the number of steps needed is a fixed constant that…
- A.
O(1) and O(n)
- B.
O(1) and O(1)
- C.
O(n) and O(1)
- D.
O(n) and O(n)
Attempted by 474 students.
Show answer & explanation
Correct answer: A
Concept: Big-O measures how the number of steps an algorithm takes grows as the input size n grows. If the number of steps needed is a fixed constant that stays the same no matter how large n becomes, that cost is O(1). If the number of steps genuinely increases in proportion to n — because the algorithm is forced to look at every element to get its answer — that cost is O(n). A singly linked list only stores a forward "next" pointer at each node; it has no backward pointer and no stored length, so any operation that needs to know "how far from the end" first needs a way to establish where the list actually ends.
Application: walking through both cases for a list of n > 8 nodes.
8th node from the head: start at the head (node 1) and follow "next" 7 times to land on node 8. That is exactly 7 pointer hops, and this count never changes even if n grows to a thousand or a million — it only depends on the fixed number 8, not on n. So this part of the work is O(1).
8th node from the end: this is the same as the (n-7)th node from the head, but n is not known in advance. Since there is no backward pointer, the only way to find out where the list ends (and hence where n-7 falls) is to walk from the head all the way to the last node once — that single pass already touches all n nodes.
After that first pass tells you n, you either re-traverse (n-7) nodes from the head, or use the two-pointer technique — advance a lead pointer 8 nodes ahead of a trail pointer, then move both one step at a time until the lead falls off the end, at which point the trail sits on the target node. Either way, the total number of pointer moves is proportional to n, because the algorithm is forced to visit every node to locate a position measured from the end.
Cross-check: with the two-pointer method, the lead pointer walks all the way from the head to past the last node — that alone is n moves — while the trail pointer follows behind for n-8 of those moves before landing on the target node; both pointers' move counts scale directly with n, confirming the end-relative case is O(n) even when a single combined pass is used instead of two separate passes. For the head-relative case, by contrast, the step count (7 hops) does not depend on n at all — whether n is 9 or 9,000,000 the walk from the head is exactly 7 hops, which is the defining property of O(1).
Result: finding the 8th element from the beginning is O(1), and finding the 8th element from the end is O(n).