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)
Inserting at the head
A new element can go into a linked list in three places: the front, the end or the middle. This lesson covers the shortest of the three, adding a new node at the front. Inserting at the head sits underneath many other structures, such as a stack built on a linked list.
- In which order the new node is linked in, and why the order matters
- Why the operation does not depend on the length of the list, that is, why it is O(1)
- How the same operation is written in C, Java and Python, and the most common mistake in C
1Idea
The head pointer holds the start of the list. To put a new node in front, we first point the new node's next field at the old first node, then move head to the new node. We never need to traverse the rest of the list.
The order matters. If we change head first, we lose the address of the old first node and can never reach the rest of the list again.
2Step by step
The drawing adds 5 to the front of a three-node list holding 12, 7 and 3. At each step the matching line of code lights up.
- Start. head points to the first node, which holds 12. The list has three nodes.
- Memory is allocated for the new node (malloc) and 5 is written into it. The node is not linked to the list yet; its
nextfield holds nothing meaningful. - The value of head is written into the new node's next field. The new node now points to the old first node (12).
- head is moved to the new node. 5 is now the first element.
- Result: 5 → 12 → 7 → 3. None of the other nodes were touched.
struct Node { int data; struct Node* next;}; void insertAtHead(struct Node** head, int value) { struct Node* node = malloc(sizeof(struct Node)); node->data = value; node->next = *head; *head = node;}class Node { int data; Node next; Node(int data) { this.data = data; }} Node insertAtHead(Node head, int value) { Node node = new Node(value); node.next = head; return node; // the new head}class Node: def __init__(self, data): self.data = data self.next = None def insert_at_head(head, value): node = Node(value) node.next = head return node # the new headThe snippets focus on the operation in this lesson. The C examples need stdlib.h.
3Complexity
Independent of the number of nodes: one allocation and two assignments.
Space is allocated only for the new node.
Compare: when inserting at the end without a pointer to the last node, you have to traverse the whole list, which makes that operation O(n).
4Common mistake
The most common mistake in C is passing head by value:
void insertAtHead(struct Node* head, int value)This function changes a copy of head. The caller's head still points to the old node and the new node is lost. So either pass struct Node** or return the new head. Pointers to pointers are covered in Memory, addresses and pointers.
5Try it
What happens if we insert at the head while the list is empty (head = NULL)?
The answer is B. The next line copies the value of head, which is NULL, and you get a correct one-node list. Inserting at the head has no special case for an empty list. The reverse operation is in Deleting from the head.
Show the answer
The answer is B. The next line copies the value of head, which is NULL, and you get a correct one-node list. Inserting at the head has no special case for an empty list. The reverse operation is in Deleting from the head.
If we swap the two assignments and write head = node first, then node->next = head, what happens?
Lessons you mark show up in the curriculum. This is kept on this device only.