Linked lists
Computer Science · Data structures
Question 1 of 10
0In the doubly linked list diagram, what is the field labelled P called?
Unlocks at a streak of 5 correct answers in a row.
Embed this practice on your site or class page
Paste this HTML into your page. Students practise right there; it is free and needs no account.
Linked lists explained
A linked list is a collection of nodes, where each node stores a value and a pointer, which is a reference to the next node. Unlike an array, the nodes do not need to sit side by side in memory. The list is entered through a head pointer, and the last node points to nothing, usually shown as null.
Inserting or deleting a node only changes a few pointers, so adding an item at the start is quick. The cost is that to reach the fifth node you must follow the pointers from the head, one node at a time. Finding an item by its position is therefore slower than with an array, where the index takes you straight to the item.
A common mistake is overwriting a pointer before saving where it pointed. When inserting, first make the new node point at the old next node, then change the previous node to point at the new node. Doing it in the other order loses the rest of the list.
Worked example
Start with the list 7, then 2, then 9, where the head points to 7. Insert the value 5 between 7 and 2.
- Create a new node holding 5.
- Point the new node at 2, the old next node, so that 2 and 9 are not lost.
- Point 7 at the new node. Only 2 pointers changed.
The list is now 7, then 5, then 2, then 9. It has nodes, and 9 is still reachable from the head.
What you will practise
40 diagrams. Type the label for each part and get instant feedback.
Example questions
- In this singly linked list, what is the box labelled A called?
- In the first node of this list, what is the left-hand part labelled B called?
- In the first node of this list, what is the right-hand part labelled C called?
- What value is stored in the node labelled F in the first list diagram?
- What does the pointer labelled E in the last node of the first list contain?
- In the doubly linked list diagram, what is the box labelled S called?
- In the doubly linked list diagram, what is the field labelled P called?
- What kind of linked list is drawn here, where the last node points back to the first?
…and 32 more.