Lesson 2 · Unit 03
Insert and delete
The order of pointer updates, and the dummy head
What you practise
- Order of pointer updates
- The dummy head trick
Try a question from this lesson
Pick one
In a chain, A points to B. To put X between them you must change two pointers. Which change has to come first?
Correct answer
Point X at B
Why
A's pointer is your only record of where B is. Point A at X first and B is gone, along with everything after it. So aim X at B while you can still find B, and only then point A at X.
The idea
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
A dummy node in front
The head is awkward because nothing points to it. So add something that does: a dummy node whose next is the real head.
Now every real node has a node before it, and one piece of code handles them all. When you finish, the list starts at dummy.next, which is correct even if the old head was deleted.
11 more exercises in the app
- 1× order the lines
- 3× type the answer
- 4× multiple choice
- 2× fill the blanks
- 1× true or false
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.