Closed Tram Stop
A tram route is stored as a chain of stops from the first stop to the terminus. Each stop knows only the next one.
Roadworks close a stop, and the city's notice counts from the far end: "the 3rd stop before the end of the line, counting the terminus as 1st". The route system must skip that stop, reading the long, shared route only once, from the front.
Your task
Write closeStop(head, k). Remove the k-th node counting from the end of the list (k = 1 is the last node) and return the head of the updated list.
If the removed stop was the first one, the head changes. If it was the only stop, return an empty list (null / None). Try to do it in a single pass.
closeStop(head: ListNode, k: int) → ListNode
Examples
head = [11, 24, 35, 47, 52] k = 2
[11, 24, 35, 52]
From the end, 52 is 1st and 47 is 2nd. Remove 47, so 35 now points straight to 52.
head = [11, 24, 35] k = 3
[24, 35]
The 3rd stop from the end of a 3-stop line is the first stop. No stop before it needs relinking, so the new head is 24.
Constraints
- 1 ≤ number of stops ≤ 5,000
- 1 ≤ k ≤ number of stops
- Stop ids are integers and may repeat.
Start with brute force
Walk the route once to count the stops, n. The closed stop is number n - k + 1 from the front, so walk again to the stop before it and relink.
That is O(n) and correct, but it reads the route twice. The gap trick does it in one pass, and the same idea finds "the k-th last item" of any stream you can read only once.
Hints
0 of 4 shown- Hint 1
To remove a node from a singly linked list, which node do you need to be standing on?
- Hint 2
The node just before the closed stop, so you can set its
nextto skip one. Knowing the lengthn, it isn - ksteps in. Can you find it withoutn? - Hint 3
Imagine two walkers on the route with exactly
kstops between them. When the front walker steps off the end, where is the back walker? - Hint 4
Put a dummy node before the head (it handles removing the first stop). Move
leadksteps from the dummy, then moveleadandtrailtogether untillead.nextis null. Nowtrail.nextis the closed stop: settrail.next = trail.next.nextand returndummy.next.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
Keep two pointers a fixed distance apart. When the front one reaches the last stop, the back one is k stops behind it, which is the stop just before the one to remove.
Trace 11 → 24 → 35 → 47 → 52 with k = 2. Start both pointers on a dummy node D placed before 11.
- Move
lead2 steps (D → 11 → 24), so lead is on 24 and trail is still on D. - Because lead.next exists, move both pointers, so lead is on 35 and trail is on 11.
- Move both again, so lead is on 47 and trail is on 24.
- Move both once more, so lead is on 52 and trail is on 35. Now lead.next is null, so the loop stops.
trail.nextis 47, so set 35's next to 52 and returndummy.next, which is the list11 → 24 → 35 → 52.
The dummy node handles the case where k equals the length. The head is removed then, and it has no node before it. With a dummy, trail stays on D and D.next = D.next.next removes the head like any other node. Forgetting this case is the most common bug in this problem.
function closeStop(head, k) {
const dummy = new ListNode(0, head); // lets us remove the first stop too
let lead = dummy;
let trail = dummy;
for (let i = 0; i < k; i++) lead = lead.next; // open a gap of k
while (lead.next !== null) {
lead = lead.next;
trail = trail.next;
}
trail.next = trail.next.next; // skip the closed stop
return dummy.next;
}def close_stop(head, k):
dummy = ListNode(0, head) # lets us remove the first stop too
lead = trail = dummy
for _ in range(k): # open a gap of k
lead = lead.next
while lead.next is not None:
lead = lead.next
trail = trail.next
trail.next = trail.next.next # skip the closed stop
return dummy.nextRun your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.