Search⌘ K

Singly Linked List - Insertion

Explore how to perform insertion operations on a singly linked list in C++. Learn to insert nodes at the head, at a specific position, and at the end. Understand the underlying process and complexity involved in these operations to manage linked lists effectively.

Structure

Each node contains a value and a pointer to the next node.

C++
struct Node {
int val;
Node* next;
Node (int val) {
this->val = val;
this->next = NULL;
}
}

Insertion

There are 3 cases:

  • Insert at beginning
  • Insert at some given position
  • Insert at end

The worst-case complexity of insertion in a linked list is O(N)O(N) ...