How to Insert in Linked List: Simple Steps and Examples
To insert in a
linked list, create a new node and adjust pointers to place it at the desired position (start, middle, or end). This involves changing the next pointer of the new node and the previous node to maintain the list structure.Syntax
Insertion in a linked list involves these steps:
- Create a new node with the data to insert.
- Set the new node's
nextpointer to the node currently at the insertion position. - Update the previous node's
nextpointer to point to the new node.
This keeps the list connected and includes the new node.
python
class Node: def __init__(self, data): self.data = data self.next = None def insert_at_start(head, data): new_node = Node(data) new_node.next = head return new_node def insert_after_node(prev_node, data): if not prev_node: return new_node = Node(data) new_node.next = prev_node.next prev_node.next = new_node def insert_at_end(head, data): new_node = Node(data) if not head: return new_node last = head while last.next: last = last.next last.next = new_node return head
Example
This example shows how to insert nodes at the start, after a given node, and at the end of a linked list.
python
class Node: def __init__(self, data): self.data = data self.next = None def print_list(head): current = head while current: print(current.data, end=' -> ' if current.next else '') current = current.next print() def insert_at_start(head, data): new_node = Node(data) new_node.next = head return new_node def insert_after_node(prev_node, data): if not prev_node: print("Previous node is None, cannot insert.") return new_node = Node(data) new_node.next = prev_node.next prev_node.next = new_node def insert_at_end(head, data): new_node = Node(data) if not head: return new_node last = head while last.next: last = last.next last.next = new_node return head # Create initial list head = Node(1) second = Node(2) third = Node(3) head.next = second second.next = third print("Original list:") print_list(head) # Insert at start head = insert_at_start(head, 0) print("After inserting 0 at start:") print_list(head) # Insert after second node insert_after_node(second, 2.5) print("After inserting 2.5 after second node:") print_list(head) # Insert at end head = insert_at_end(head, 4) print("After inserting 4 at end:") print_list(head)
Output
Original list:
1 -> 2 -> 3
After inserting 0 at start:
0 -> 1 -> 2 -> 3
After inserting 2.5 after second node:
0 -> 1 -> 2 -> 2.5 -> 3
After inserting 4 at end:
0 -> 1 -> 2 -> 2.5 -> 3 -> 4
Common Pitfalls
Common mistakes when inserting in linked lists include:
- Not updating the
nextpointer of the new node correctly, which breaks the list. - Forgetting to handle the case when the list is empty (head is
None). - Trying to insert after a
Nonenode, which causes errors. - Not returning the new head when inserting at the start, leading to lost nodes.
python
def wrong_insert_at_start(head, data): new_node = Node(data) # Forgot to link new_node.next to head return new_node # This loses the rest of the list # Correct way def correct_insert_at_start(head, data): new_node = Node(data) new_node.next = head return new_node
Quick Reference
- Insert at start: New node's next points to current head; update head.
- Insert after node: New node's next points to previous node's next; previous node's next updated to new node.
- Insert at end: Traverse to last node; last node's next points to new node.
Key Takeaways
Always create a new node and adjust pointers carefully to insert in a linked list.
Handle special cases like inserting at the start or into an empty list by updating the head.
Never forget to link the new node's next pointer to maintain the list chain.
Check that the previous node exists before inserting after it to avoid errors.
Returning the updated head is essential when inserting at the beginning.