Use fast and slow pointers to show the middle value.
Acceptance criteria
- 1.#out should show middle 3.
DSA Tutorial · Linked Lists
These 10 tasks go with the Linked List Two Pointer Problems chapter. 5 of them ask you to build something small from scratch; 5 give you code that is already broken and ask you to work out why. Set aside about 90 minutes for the whole set, though you can do them in any order.
Nothing here is marked by a person. When you press Submit, the editor checks your code against the points listed under each task and tells you straight away what passed and what didn't (you need a free account to submit). If you get stuck, there are hints, and a worked solution once you've had a go. The first 2 tasks are free; the rest are part of the practice plan.
If it's been a while since you read the chapter, here is how it starts: “You cannot index into a linked list, so every question about position is answered with two pointers moving at different speeds. Learn the three shapes and you have covered most of what gets asked.” .
1.Find the middleexercise · easy · 10 min
Use fast and slow pointers to show the middle value.
Passes when: #out should show middle 3.
2.Detect no cycleexercise · easy · 10 min · practice plan
Report whether the list has a cycle.
Passes when: #out should show no cycle.
3.Detect a cycleexercise · easy · 10 min · practice plan
Build a looping list and report that a cycle exists.
Passes when: #out should show has a cycle.
4.Nth from the endexercise · easy · 10 min · practice plan
Show the second-from-last value using a gap of two.
Passes when: #out should show nth 3.
5.Even length middleexercise · medium · 10 min · practice plan
On an even list the fast pointer stops past the end — show the value reached.
Passes when: #out should show middle 3.
6.Fix the fast pointer moving one stepbug fix · easy · 8 min
Both pointers at the same speed never separate.
Passes when: #out should show middle 3.
7.Fix the missing next guardbug fix · medium · 8 min · practice plan
Without checking fast.next, the two-step move reads through null.
Passes when: #out should show no cycle.
8.Fix the cycle check comparing valuesbug fix · easy · 8 min · practice plan
Two nodes can hold equal values without being the same node.
Passes when: #out should show no cycle.
9.Fix the gap set to the wrong sizebug fix · easy · 8 min · practice plan
A gap of one finds the last node rather than the second from last.
Passes when: #out should show nth 3.
10.Fix both pointers starting apartbug fix · easy · 8 min · practice plan
Starting slow one node in shifts the middle by one.
Passes when: #out should show middle 3.
Between them these tasks cover Fast and slow, Cycle detection and Nth from end.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.