Linked list operations manipulate pointers to insert, delete, and traverse nodes. The key is understanding how pointers break and reform connections — like rearranging a chain by changing which links connect to which.
Inserting at the head is like cutting in line at the front — you just tell the new person to point to the old first person, and update the 'head' marker. No one else needs to move!
Watch how links break and reform.
1// Printing the Scavenger Hunt2struct Node* temp = head;34while (temp != NULL) {5printf("%d -> ", temp->data);6temp = temp->next; // Move to next clue7}8printf("NULL");
Traversal: Following the Chain
To visit every node, you start at head and follow pointers:
temp = head;
while (temp != NULL) {
// Do something with temp->data
temp = temp->next; // Move to next node
}
The magic is temp = temp->next — this doesn't move the node, it moves your reference to point at the next node's address.
Insertion at Head: O(1)
1. Create new node
2. Set new node's next to current head
3. Update head to point to new node
No traversal needed — instant!
Insertion at Tail: O(n)
You must traverse to find the last node (the one with next == NULL), then link it. This is why tail insertion is slower.
Deletion: The Pointer Dance
To delete a node, you must update the previous node's next pointer to skip the deleted node. In singly linked lists, you need to keep track of the previous node during traversal.
Mastering pointer manipulation is essential for linked lists. Unlike arrays where you shift elements, linked lists just redirect pointers. This makes insertions/deletions at the head extremely fast (O(1)).
"What is the time complexity of inserting a node at the beginning of a linked list?"
O(1) — constant time. You just create the node, set its next to head, and update head. No traversal needed.