Skip to content
Home » Inserting in linked list

Inserting in linked list

✅ Insertion in Linked List


✅ 1. Introduction

Insertion in a linked list means adding a new node at a desired position in the list.
Since linked lists are dynamic, insertion can be done easily without shifting elements, unlike arrays.


✅ 2. Types of Insertion in Linked List

Insertion in a linked list can be performed at:

  1. Insertion at Beginning
  2. Insertion at End
  3. Insertion at a Specific Position (Middle)

These operations are explained below for a Singly Linked List.


✅ 3. Insertion at Beginning

✅ Definition

Insertion at beginning means adding a new node before the first node of the linked list.


✅ Steps Involved

  1. Create a new node NEW
  2. Store data in NEW->data
  3. Set NEW->next = HEAD
  4. Update HEAD = NEW

✅ Diagram

Before insertion:

HEAD → 10 → 20 → 30 → NULL

After inserting 5:

HEAD → 5 → 10 → 20 → 30 → NULL

✅ Algorithm: Insert_Begin(HEAD, ITEM)

  1. Start
  2. Create new node NEW
  3. Set NEW->data = ITEM
  4. Set NEW->next = HEAD
  5. Set HEAD = NEW
  6. Stop

✅ Time Complexity

O(1) (fastest insertion)


✅ 4. Insertion at End

✅ Definition

Insertion at end means adding a new node after the last node of the linked list.


✅ Steps Involved

  1. Create a new node NEW
  2. Set NEW->data = ITEM
  3. Set NEW->next = NULL
  4. Traverse the list to reach the last node
  5. Set last->next = NEW

✅ Diagram

Before insertion:

HEAD → 10 → 20 → 30 → NULL

After inserting 40:

HEAD → 10 → 20 → 30 → 40 → NULL

✅ Algorithm: Insert_End(HEAD, ITEM)

  1. Start
  2. Create new node NEW
  3. Set NEW->data = ITEM
  4. Set NEW->next = NULL
  5. If HEAD == NULL, then
    • Set HEAD = NEW and Stop
  6. Set temp = HEAD
  7. While temp->next != NULL
    • temp = temp->next
  8. Set temp->next = NEW
  9. Stop

✅ Time Complexity

O(n) (traversal required)


✅ 5. Insertion at a Specific Position (Middle)

✅ Definition

Insertion at a specific position means adding a new node after a given node or at a given position in the linked list.


✅ Steps Involved

  1. Create new node NEW
  2. Traverse to the node just before the required position
  3. Set NEW->next = temp->next
  4. Set temp->next = NEW

✅ Diagram

Insert 25 after 20:

Before:

HEAD → 10 → 20 → 30 → 40 → NULL

After:

HEAD → 10 → 20 → 25 → 30 → 40 → NULL

✅ Algorithm: Insert_Position(HEAD, ITEM, POS)

  1. Start
  2. Create new node NEW
  3. Set NEW->data = ITEM
  4. If POS == 1, then
    • Perform insertion at beginning
  5. Set temp = HEAD
  6. Repeat POS-2 times:
    • temp = temp->next
  7. Set NEW->next = temp->next
  8. Set temp->next = NEW
  9. Stop

✅ Time Complexity

O(n)


✅ 6. Special Case: Insertion in Circular Linked List (Brief)

  • Insertion at beginning or end requires updating last node pointer.
  • No NULL pointer exists.

✅ 7. Advantages of Insertion in Linked List

✅ Benefits:

  • No shifting of elements
  • Efficient insertion compared to arrays
  • Dynamic memory usage

✅ Conclusion

Insertion in a linked list is a simple and efficient operation that involves adjusting pointers. Depending on the position, insertion can be done at the beginning (O(1)), end (O(n)), or middle (O(n)). Linked lists are preferred when frequent insertions are required.