Introduction
Insertion and deletion are the most important operations in a Linked List.
The task is to:
- insert new nodes
- delete existing nodes
- maintain correct links between nodes
Example:
Input:1 -> 2 -> 4
Insert:
3
Output:
1 -> 2 -> 3 -> 4
Example:
Input:1 -> 2 -> 3 -> 4
Delete:
3
Output:
1 -> 2 -> 4
This problem is one of the most important basics of:
Linked List Data Structure Constraints
1 <= Number of Nodes <= 10^5-10^9 <= Node Value <= 10^9Approach 1 : Brute Force (Using Array Conversion)
Explanations:
Explanation:
The idea is:
- convert linked list into array
- perform insert/delete operation
- rebuild linked list
Steps:
- Traverse linked list.
- Store values into array.
- Insert/Delete value.
- Create new linked list.
This approach works but:
- uses unnecessary extra space
- rebuilding linked list is inefficient
So direct pointer manipulation is preferred.
Dry Run
Input:1 -> 2 -> 4
Insert:
3
Step 1:
Array:
[1,2,4]
Step 2:
Insert 3
Array:
[1,2,3,4]
Step 3:
Rebuild linked list
Output:
1 -> 2 -> 3 -> 4
Practice :
Complexity Analysis :
Time Complexity:- O(n)Explanation :
Linked list traversal is required.
Space Complexity:- O(n)
Explanation :
Extra array is used.
Approach 2 : Optimal Solution(Direct Pointer Manipulation)
Explanations:
Explanation:
This is the most optimized and interview-preferred solution.
The idea is:
- directly manipulate node pointers
- connect new node
- bypass deleted node
This avoids extra space usage.
Dry Run
Input:1 -> 2 -> 4
Insert:
3 after 2
Step 1:
Create new node:
3
Step 2:
3.next = 4
Step 3:
2.next = 3
Output:
1 -> 2 -> 3 -> 4
Practice :
Complexity Analysis :
Time Complexity:- O(1)Explanation :
Insertion and deletion occur directly using pointers.
Space Complexity:- O(1)
Explanation :
No extra space is used.
Why This Problem is Important
This problem builds the foundation for:
- Pointer manipulation
- Dynamic memory handling
- Linked List operations
- Node connection logic
- Data structure traversal
Real-World Applications
Insertion and deletion in linked lists are used in:
- Browser history
- Music playlists
- Undo/Redo systems
- Memory allocation
- Dynamic scheduling systems
Common Beginner Mistakes
- Losing node references
- Incorrect pointer updates
- Forgetting next connections
- Breaking linked list structure
- NULL pointer errors
Interview Tip
Interviewers often expect:
- correct pointer manipulation
- proper node insertion logic
- clean deletion handling
- O(1) operations
Always explain:
- how pointers are reconnected
- why linked lists support efficient insertion/deletion
Related Questions
- Traverse Linked List
- Search in Linked List
- Reverse Linked List
- Middle of Linked List
- Detect Cycle in Linked List
Final Takeaway
The Insert/Delete Node problem is one of the most important beginner linked list problems.
It teaches:
- pointer manipulation
- node insertion
- node deletion
- dynamic structure updates
Understanding this problem builds a strong foundation for:
- advanced linked list algorithms
- pointer-based problems
- interview-level data structure questions.