Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

# Insertion at the Beginning of a Singly Linked List in C++

Last Updated on May 9, 2023 by Prepbytes

One of the operations that we may carry out on a Linked List in C++ is Insertion at Beginning. Node and pointer are the two components that make up linked, where the node holds the data and the pointer holds the address of the subsequent node.

## What is a Singly Linked List?

A linked list is a linear data structure with several nodes that are linked to one another. Each node has two fields: the data field and the following node’s address. So, in this article, we’ll look at how to implement insertion at the start of a singly linked list in C++. Here is a template for a singly linked list Node that we create.

``````class Node
{
int data;
Node *next;
};``````

## How to Insert at the Beginning of a Singly Linked List in C++

Assume you’ve been given a single linked list.The goal is to place a node at the start of the given linked list.

For example,
Input :

``````10 β 20 β 30 β 40 β 50
Insert β0β at the head or beginning of the given linked list.``````

Output :

``0 β 10 β 20 β 30 β 40β 50``

It will print the linked list after inserting the node at the beginning of it as
0 β 10 β 20 β 30 β 40β 50

### Algorithm for Insertion at the Beginning of a Singly Linked List

Below mentioned steps can be followed for insertion at the beginning of a singly linked list.

1. Make a new node using the given data.
3. Set the new node’s next pointer to the current head of the linked list.
4. Set the linked list’s head to point to the new node.
5. Return.

### Pseudo Code

``````function insertAtBeginning(data):
newNode = Node(data)
return
return``````

Now let us discuss the code implementation of insertion at the beginning of a singly linked list in C++.

### Code Implementation in C++

```#include <iostream>
struct Node {
int data;
Node* next;
};

void insertAtBeginning(Node*& head, int data) {
Node* newNode = new Node{data, head};
}

int main() {
insertAtBeginning(head, 10); // insert a node with data 10
insertAtBeginning(head, 20); // insert a node with data 20
insertAtBeginning(head, 30); // insert a node with data 30

for (Node* curr = head; curr != NULL; curr = curr->next) {
std::cout << curr->data << " ";
}
// expected output: 30 20 10

return 0;
}
```

Output :

``30 20 10 ``

Conclusion
In this article, we have discussed how we can perform insertion at the beginning of a singly linked list in C++, along with the steps and proper pseudocode. The code implementation has also been discussed in this article.

Q1. How to perform insertion at the beginning in a Singly Linked List in C++?
Ans.

• Make memory for the new Node.
• Assign a value to the data.
• Assign the current head to the newNode next.
• Replace the head with this newNode.

Q2. What is insertion and deletion operation in singly linked list?
Ans. Insertion inserts an element at the start of the list. Deletion removes an element from the list’s beginning.

Q3. What are the ways to insert a node in a linked list?
Ans.