Linked lists · Exercise 20 of 100
Build a circular linked list
In a nonempty circular list, the tail links back to the head, so even a lone node links to itself. To append a node, link it to the head and link the old tail to it. The new node is now the tail, and its link closes the circle.
Try it yourself
A circle starts at head 4, then visits 10 and tail 13. Append 17 as the new tail. If you start at 13 and follow exactly two next links, which value do you reach?
One-step hint
Redraw the old tail's outgoing arrow after the append and give the new tail an arrow back to the head. Trace from the old tail.
Worked solution
- Set the next link of 17 to head 4, then change the next link of 13 to 17.
- The first link from 13 takes you to 17. The second wraps around to 4.