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)
Creating a singly linked list
In What is a singly linked list? you saw what a node looks like. Now we build a small list by hand: 12 → 7 → 3. This lesson is the starting point of every list operation; the drawings in the following lessons all start from this list.
1Idea
Building a linked list takes three jobs. First, memory is allocated for each node and its value is written. Then the address of the following node is written into each node's next field. Finally head is pointed at the first node.
In C, memory comes from malloc and the node is defined as a struct. In Java and Python, writing new Node(12) or Node(12) both allocates the memory and writes the value. In those two languages the next field already starts as null or None; in C it holds an unknown value, so NULL is written into the last node's next field explicitly.
Before the list is built, head points to NULL. That is the empty list: there are no nodes and head points nowhere.
2Step by step
The drawing allocates three nodes holding 12, 7 and 3, links them together and sets head to the first node. At each step the matching line of code lights up.
- Start. There are no nodes yet; head points to NULL. The list is empty.
- Memory is allocated for three nodes and their values are written: 12, 7 and 3. The nodes sit in separate places in memory with no links between them yet. Their
nextfields are unknown. first->next = second: the node holding 12 now points to 7.second->next = third: the node holding 7 points to 3. The chain grows.- NULL is written into the last node's next field. Code that walks (traverses) the list knows from this value where to stop.
- head is set to the first node. From now on the list is always reached through
head. - Result: 12 → 7 → 3 → NULL. The helper variables (
first,second,third) are no longer needed; the list is held byhead.
struct Node { int data; struct Node* next;}; void createList(struct Node** head) { struct Node* first = malloc(sizeof(struct Node)); struct Node* second = malloc(sizeof(struct Node)); struct Node* third = malloc(sizeof(struct Node)); first->data = 12; second->data = 7; third->data = 3; first->next = second; second->next = third; third->next = NULL; // last node *head = first;}class Node { int data; Node next; Node(int data) { this.data = data; }} Node createList() { Node first = new Node(12); Node second = new Node(7); Node third = new Node(3); first.next = second; second.next = third; third.next = null; // last node (already null) Node head = first; return head;}class Node: def __init__(self, data): self.data = data self.next = None def create_list(): first = Node(12) second = Node(7) third = Node(3) first.next = second second.next = third third.next = None # last node (already None) head = first return headThe snippets focus on the operation in this lesson. The C examples need stdlib.h.
3Complexity
For a list of n nodes, each node takes one allocation and one link assignment. In this example n = 3.
Each node takes room for one value and one pointer. Memory grows in proportion to the number of nodes.
Real programs do not write the nodes out one by one; they call insert at the head or insert at the end inside a loop.
4Common mistake
The most common mistake in C is leaving the last node's next field unset:
struct Node* third = malloc(sizeof(struct Node));third->data = 3;// third->next = NULL; is missingmalloc does not clear the memory. A random address stays in next, and code that walks the list jumps to that address and crashes or reads garbage. Always write NULL into the last node's next field.
5Try it
You allocated and linked three nodes but forgot the line head = first. What state is the list in?
The answer is A. Until head is set there is no way to reach the list from outside. When the function returns, the helper variables are gone too, and the nodes stay in memory with nothing pointing to them.
Show the answer
The answer is A. Until head is set there is no way to reach the list from outside. When the function returns, the helper variables are gone too, and the nodes stay in memory with nothing pointing to them.
Does the order of linking matter? What if you write second->next = third first and then first->next = second?
The answer is B. Here every node's address lives in its own variable, so the links can be set in any order. Adding to an existing list is different; when inserting at a position the order matters.
Show the answer
The answer is B. Here every node's address lives in its own variable, so the links can be set in any order. Adding to an existing list is different; when inserting at a position the order matters.
Lessons you mark show up in the curriculum. This is kept on this device only.