Now that we have some data, we need to be able to remove it. In a linked list, the easiest place to work is at the start of the list as that is where we have direct access via the head pointer. So we will focus on removing the first node.
The animation is set to remove the first node from the list. Use > to step through the removal process. Then press Delete Front and continue stepping to delete the second node.
You can click and drag on the animation area to pan around. Use the Ctrl + mouse wheel (or touchpad scroll gesture) while over the animation area to zoom in and out.
Notice that we need a pointer to keep track of the node that we will be deleting after we update head. If we just do head = head->next;, we will lose the address of the old first node and will not be able to delete it:
If we try to delete the head node without saving a pointer to the next node, we are in even worse shape because we will lose the address of the 10 node:
We then delete the node that toDelete is pointing at. Then, as we leave the removeStart function, the toDelete variable goes out of scope. As long as we have called delete toDelete; before leaving the function, the memory used by the deleted node will also have been cleaned up.
This logic works for both the case where there are multiple nodes in the list and the case where there is only one node in the list. If there is currently just one node in the list, head will end up pointing to nullptr, which correctly indicates that the list is now empty.