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 end
Inserting at the head does not depend on the length of the list. Inserting at the end is different: to find the end you first have to walk the whole list. This lesson shows how to link a new node at the end of the list and why the empty list is handled separately.
1Idea
To link a new node at the end, it is enough to write its address into the last node's next field. The problem is that we do not know where the last node is. The list keeps only head, so we start at the front and move until we find the node whose next field is NULL.
This walk is like traversal with one difference: the loop runs on temp->next != NULL, not temp != NULL. That way temp stops on the last node before falling off to NULL, and the new node can be linked to it.
If the list is empty there is no last node and temp->next cannot be read. In that case the new node becomes both the first and the last node, so head points to it. Because head changes, the C function takes struct Node**.
If you keep a tail pointer to the last node, there is no walk: you write tail->next = node; tail = node; and the operation is O(1). In return you must keep tail up to date on every insertion and deletion.
2Step by step
The drawing adds 9 to the end of the list holding 12, 7 and 3. At each step the matching line of code lights up.
- Start. head points to the first node (12). The next field of the last node, 3, is NULL.
- Memory is allocated for the new node (malloc) and 9 is written into it. It will go to the end, so its
nextfield is NULL. - The list is not empty.
temptakes the value of head and starts walking from the first node. - 12's
nextfield is not NULL;tempmoves one node and points to 7. tempmoves to 3. 3's next field is NULL, so the loop stops here:tempis on the last node.- The new node's address is written into the last node's
nextfield. 3 now points to 9. - Result: 12 → 7 → 3 → 9 → NULL. head did not change; only the last node's
nextfield was updated.
struct Node { int data; struct Node* next;}; void insertAtEnd(struct Node** head, int value) { struct Node* node = malloc(sizeof(struct Node)); node->data = value; node->next = NULL; // the new node will be last if (*head == NULL) { // empty list *head = node; return; } struct Node* temp = *head; while (temp->next != NULL) temp = temp->next; temp->next = node;}class Node { int data; Node next; Node(int data) { this.data = data; }} Node insertAtEnd(Node head, int value) { Node node = new Node(value); if (head == null) return node; // empty list Node temp = head; while (temp.next != null) temp = temp.next; temp.next = node; return head;}class Node: def __init__(self, data): self.data = data self.next = None def insert_at_end(head, value): node = Node(value) if head is None: # empty list return node temp = head while temp.next is not None: temp = temp.next temp.next = node return headThe snippets focus on the operation in this lesson. The C examples need stdlib.h.
3Complexity
Finding the last node means passing all n nodes. The linking itself takes constant time.
Apart from the new node, only one helper pointer is used.
With a tail pointer the walk disappears and inserting at the end becomes O(1). A queue built on a linked list keeps tail for exactly this reason.
4Common mistake
A common mistake is forgetting the empty list:
struct Node* temp = *head;while (temp->next != NULL) // temp is NULL if the list is empty temp = temp->next;temp->next = node;When the list is empty, temp is NULL and reading temp->next crashes the program. Check *head == NULL before the loop, and in that case point head at the new node.
5Try it
What happens if you change the loop condition to temp != NULL?
The answer is B. The temp != NULL condition walks past the last node too and moves temp to NULL. The node we wanted to link to is lost. To stop on the last node you need temp->next != NULL.
Show the answer
The answer is B. The temp != NULL condition walks past the last node too and moves temp to NULL. The node we wanted to link to is lost. To stop on the last node you need temp->next != NULL.
On a list of 1000 nodes with no tail pointer, how many nodes are passed to insert at the end?
Lessons you mark show up in the curriculum. This is kept on this device only.