# Insert Node at the Middle of the Linked List

Insert Node at the Middle of the Linked List” is a basic programming problem based on linked list. Here, we are given a linked list and our task is to insert the new node at the middle of the linked list.

```Examples:
1 -> 2 -> 4 -> 5 -> NULL
After Inserting New Node at the Middle of the Linked List:
1 -> 2 -> 3 -> 4 -> 5 -> NULL ```

There are basically two methods to insert node at the middle of the linked list, which are:

1. Counting based method
2. Hop Jump Method

Method 1: Insert Node at the Middle of the Linked List using counting based technique

The steps required to insert node at the middle of the linked list are as follows:

1. Create a new Node with desired Value in data part and NULL in pointer part.
2. Check whether linked list is empty or not.
3. If Linked List is empty, then new node will become head of the linked list, then return.
4. If Linked is not empty, then traverse the whole linked list and count the number of nodes in the linked list.
5. Again, traverse the linked list up to count/2 nodes, known as middle node.
6. Now, pointer of new node will point to the next of the middle node and then pointer of middle node will point to the new node.

#### C++ Program to Insert node at the middle of the linked list(counting method)is as follows:

```/* C++ Program to Insert Node at the Middle of the Linked List */
#include<bits/stdc++.h>
using namespace std;
/* Structure of the Node of the Linked List */
struct node
{
int data;
struct node *next;
};

/* Function to Insert Node at the End of the Linked List */
void insert_end(struct node **head, int ele)
{
// Creating a new node.
struct node *new_node;
new_node = (struct node*)malloc(sizeof(struct node));
new_node->data = ele;
new_node->next = NULL;

/* Check whether the linked list is empty or not */

{
return;
}
/* If Linked List is not empty, then traverse the whole linked list and pointer
of last node of the linked list will point to the new node of the linked list */
struct node *temp = NULL;
while(temp->next != NULL)
temp=temp->next;
temp->next = new_node;
}

/* Function to Insert Node at the Middle of the Linked List */
{
/* Creating New Node */
struct node *new_node;
new_node = (struct node*)malloc(sizeof(struct node));
new_node->data = ele;
new_node->next = NULL;

/* Check whether the linked list is empty or not */

{
return;
}

/* Traverse the complete linked list and count the total number of node in it */
int count = 0;
while(temp!=NULL)
{
count++;
temp=temp->next;
}

/* Now, traverse to half of the linked linked (count/2) and insert the new node there*/
for(int i=1;i<(count/2);i++)
{
temp = temp -> next;
}
new_node->next = temp->next;
temp->next = new_node;
}

/* Function to Print the Linked List */
{
while(p!=NULL)
{
cout<<p->data<<" ";
p=p->next;
}
}
int main()
{
/* Inserting Some Nodes in the Linked List */
cout<<"Linked list elements before inserting new node\n";

/* Inserting New Node at the Middle of the Linked List */

cout<<"\nLinked list elements after inserting new node \n";
}
```
```OUTPUT:
Linked list elements before inserting new node
1 2 1 3
Linked list elements after inserting new node
1 2 5 1 3```

Method 2: Insert node at the middle of the linked list using hop jump technique

The steps required to insert node at the middle of the linked list are as follows:

1. Initialize two pointer; slow pointer and fast pointer pointing to the head of the linked list.
2. Now, traverse the whole linked list, slow pointer will increment to next node while fast pointer will increment to next to next node.
3. Once, the fast pointer reaches to the end of the list, the slow pointer will point to the middle node of linked list.
4. Now, the pointer of the new node will point to the next of the middle node and pointer of the middle node will point to the new node.

#### C++ Program to insert node at the middle of the Linked List(Hop Jump)is as follows:

```/* C++ Program to Insert Node at the Middle of Linked List */
#include<bits/stdc++.h>
using namespace std;

/* Structure of the New Node */
struct node
{
int data;
struct node *next;
};

/* Function to Insert Node at the End of the Linked List */
void insert_end(struct node **head, int ele)
{
// Creating a new node.
struct node *new_node;
new_node = (struct node*)malloc(sizeof(struct node));
new_node->data = ele;
new_node->next = NULL;

/* Check whether the linked list is empty or not */

/* If Linked List is Empty, then new node will become head of the linked list */
{
return;
}

/* If Linked List is not Empty, then trsaverse the completer linked list
and pointer of last node of the linked list will point to the new node */
struct node *temp = NULL;
while(temp->next != NULL)
temp=temp->next;
temp->next = new_node;
}

/* Function to Inset New Node at the Beginning of the Linked List */
{
/* Creating New Node */
struct node *new_node;
new_node = (struct node*)malloc(sizeof(struct node));
new_node->data = ele;
new_node->next = NULL;

/* If linked List is empty, then new node will become head of the Linked List */
{
return;
}

/* If Linked List is not empty,
Initilize two pointers; slow pointer which will traverse the list one node at a time,
and fast pointer which will traverse the list two nodes at the time. In this manner,
when fast pointer reaches the end of the linked list, the slow pointer points to the
middle of the linked list, where we need to insert new node.
*/
while(slow!=NULL && fast!=NULL && fast->next!=NULL && fast->next->next!=NULL)
{
slow = slow->next;
fast = fast->next->next;
}
new_node->next = slow -> next;
slow->next = new_node;
}

/* Function to print the complete linked list */
{
while(p!=NULL)
{
cout<<p->data<<" ";
p=p->next;
}
}
int main()
{
/* Inserting Some Node in the Linked List */
cout<<"Linked list elements before inserting new node: \n";

/* Inserting New Node at the middle of the Linked List */

cout<<"\nLinked list elements after inserting new node: \n";
```OUTPUT: