3.1Singly Linked List
- 3.1.1What is a singly linked list?
- 3.1.2Creating a singly linked list
- 3.1.3Traversal
- 3.1.4Inserting at the end
- 3.1.5Inserting at a position
- 3.1.6Inserting at the head
- 3.1.7Deleting from the end
- 3.1.8Deleting from the head
- 3.1.9Deleting from the middle
- 3.1.10Searching for a value
- 3.1.11Reversing a linked list (iterative)
- 3.1.12Reversing a linked list (recursive)
Traversal
In an array you jump straight to an element by its index. A linked list has no such shortcut: the only way to reach a node is to start at the front and follow the links one by one. This walk is called traversal. Many operations, such as searching, inserting at the end and deleting, contain a traversal inside them.
1Idea
To traverse, we copy the value of head into a helper pointer called temp. On each turn of the loop we print the value of the node temp points to, then move temp to the address in that node's next field.
The last node's next field is NULL, so after the last turn temp becomes NULL too and the loop ends. If the list is empty, head is already NULL; the loop does not run even once and only NULL is printed. The empty list needs no separate check.
We walk a helper pointer, not head. If we moved head itself, the address of the start of the list would be lost once the walk ended.
2Step by step
The drawing traverses the list holding 12, 7 and 3. The temp pointer moves one node at each step; the printed output is in the step captions.
temptakes the value of head and points to the first node (12).headis not touched.tempis not NULL, so we enter the loop. 12 is printed. Screen:12 ->tempmoves to the address in 12's next field and now points to 7.- 7 is printed. Screen:
12 -> 7 -> tempmoves one more node and points to the last node (3).- 3 is printed. Screen:
12 -> 7 -> 3 -> - The last node's
nextfield is NULL.tempnow points to NULL. - The loop condition is false and the loop ends.
NULLis printed at the end. Screen:12 -> 7 -> 3 -> NULL.headis still on the first node.
struct Node { int data; struct Node* next;}; void printList(struct Node* head) { struct Node* temp = head; while (temp != NULL) { printf("%d -> ", temp->data); temp = temp->next; } printf("NULL\n");}class Node { int data; Node next; Node(int data) { this.data = data; }} void printList(Node head) { Node temp = head; while (temp != null) { System.out.print(temp.data + " -> "); temp = temp.next; } System.out.println("NULL");}class Node: def __init__(self, data): self.data = data self.next = None def print_list(head): temp = head while temp is not None: print(temp.data, end=" -> ") temp = temp.next print("NULL")The snippets focus on the operation in this lesson. The C examples need stdlib.h.
3Complexity
Each node is visited once. On a list of n nodes the loop runs n times.
However long the list is, only one helper pointer is used.
Searching for a value and inserting at the end make the same walk, which is why they are also O(n) in the worst case.
4Common mistake
A common mistake is writing the loop condition as temp->next != NULL:
while (temp->next != NULL) { printf("%d -> ", temp->data); temp = temp->next;}This loop stops on the last node but never prints its value; the output is 12 -> 7 -> . Worse, if the list is empty, temp is NULL and reading temp->next crashes the program. To process every node the condition must be temp != NULL. When you need to stop on the last node (for example when inserting at the end), the next condition is right, but then the empty list is checked separately.
5Try it
When the loop ends, what does temp point to?
The answer is B. The loop only ends once temp is NULL. To stop on the last node you have to change the condition; you will see this in inserting at the end.
Show the answer
The answer is B. The loop only ends once temp is NULL. To stop on the last node you have to change the condition; you will see this in inserting at the end.
How many times does the traversal loop run on a list of 5 nodes?
The answer is B. The loop runs once for each node. On the sixth check temp is NULL and the loop is not entered.
Show the answer
The answer is B. The loop runs once for each node. On the sixth check temp is NULL and the loop is not entered.
Lessons you mark show up in the curriculum. This is kept on this device only.