How to Insert a Node in Linked List in C++
To insert a node in a linked list in C++, create a new node with
new, set its next pointer to the next node, and update the previous node's next to point to the new node. This can be done at the beginning, middle, or end by adjusting pointers accordingly.Syntax
Here is the basic syntax to insert a new node after a given node in a singly linked list:
Node* newNode = new Node(data);— create a new node with the given data.newNode->next = prevNode->next;— link new node to the next node.prevNode->next = newNode;— link previous node to the new node.
If inserting at the head, update the head pointer to the new node.
cpp
struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
// Insert newNode after prevNode
void insertAfter(Node* prevNode, int newData) {
if (prevNode == nullptr) return; // cannot insert
Node* newNode = new Node(newData);
newNode->next = prevNode->next;
prevNode->next = newNode;
}Example
This example shows how to insert nodes at the beginning, after a given node, and at the end of a linked list.
cpp
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
void insertAtHead(Node*& head, int newData) {
Node* newNode = new Node(newData);
newNode->next = head;
head = newNode;
}
void insertAfter(Node* prevNode, int newData) {
if (prevNode == nullptr) return;
Node* newNode = new Node(newData);
newNode->next = prevNode->next;
prevNode->next = newNode;
}
void insertAtEnd(Node*& head, int newData) {
Node* newNode = new Node(newData);
newNode->next = nullptr;
if (head == nullptr) {
head = newNode;
return;
}
Node* last = head;
while (last->next != nullptr) {
last = last->next;
}
last->next = newNode;
}
void printList(Node* node) {
while (node != nullptr) {
cout << node->data << " ";
node = node->next;
}
cout << endl;
}
int main() {
Node* head = nullptr;
insertAtEnd(head, 10);
insertAtHead(head, 5);
insertAtEnd(head, 15);
insertAfter(head->next, 12); // insert 12 after second node
printList(head);
return 0;
}Output
5 10 12 15
Common Pitfalls
Common mistakes when inserting nodes include:
- Not updating the
nextpointers correctly, causing broken links. - Forgetting to handle the case when the list is empty (head is
nullptr). - Inserting after a
nullptrnode, which is invalid. - Memory leaks by not using
newproperly or losing node references.
Always check pointers before dereferencing and update links carefully.
cpp
/* Wrong way: inserting after nullptr */ void insertAfterWrong(Node* prevNode, int newData) { // This will crash if prevNode is nullptr Node* newNode = new Node(newData); newNode->next = prevNode->next; // error if prevNode is nullptr prevNode->next = newNode; } /* Correct way: check for nullptr */ void insertAfterSafe(Node* prevNode, int newData) { if (prevNode == nullptr) return; // safe check Node* newNode = new Node(newData); newNode->next = prevNode->next; prevNode->next = newNode; }
Quick Reference
Tips for inserting nodes in linked lists:
- Use
newto create nodes dynamically. - Always update the
nextpointers in correct order: new node'snextfirst, then previous node'snext. - Handle special cases: empty list, insert at head, insert at end.
- Check for
nullptrbefore dereferencing pointers. - Free memory if removing nodes to avoid leaks (not shown here).
Key Takeaways
Create a new node with new and set its data before linking it.
Update pointers carefully: newNode->next first, then previousNode->next.
Handle edge cases like empty list and insertion at head separately.
Always check for nullptr before accessing node pointers.
Use helper functions to keep insertion code clean and reusable.