# Program To Delete A New Node From The Middle Of The Singly Linked List

Program To Delete A New Node From The Middle Of The Singly Linked List on fibonacci, factorial, prime, armstrong, swap, reverse, search, sort, stack, queue, array, linkedlist, tree, graph etc.

## Program to delete a new node from the middle of the singly linked list

### Explanation

In this program, we will create a singly linked list and delete a node from the middle of the list. To accomplish this task, we will calculate the size of the list and then divide it by 2 to get the mid-point of the list. Node temp will point to head node. We will iterate through the list till mid?point is reached. Now, the temp will point to middle node and node current will point to node previous to temp. We delete the middle node such that current's next node will point to temp's next node.

Consider the above example, mid-point of the above list is 2. Iterate temp from head to mid-point. Now, temp is pointing to mid node which needs to be deleted. In this case, Node is the middle node which needs to be deleted. The node can be deleted by making node 2's next (current) to point to node 3 (temp's next node). Set temp to null.

### Algorithm

1. Create a class Node which has two attributes: data and next. Next is a pointer to the next node in the list.
2. Create another class DeleteMid which has three attributes: head, tail, and size which keep tracks of the number of nodes present in the list.
1. Create a new node.
2. It first checks, whether the head is equal to null which means the list is empty.
3. If the list is empty, both head and tail will point to the newly added node.
4. If the list is not empty, the new node will be added to end of the list such that tail's next will point to the newly added node. This new node will become the new tail of the list.
4. deleteFromMid() will delete a node from the middle of the list:
1. It first checks whether the head is null (empty list) then, display the message "List is empty" and return.
2. If the list is not empty, it will check whether the list has only one node.
3. If the list has only one node, it will set both head and tail to null.
4. If the list has more than one node then, calculate the size of the list and divide it by 2 to get the mid-point of the list.
5. Declare a node temp which will point to head and node current will point to node previous to temp.
6. Traverse through the list till temp points to a middle node. If current not point to null then, delete the middle node(temp) by making current's next to point to temp's next. Else, both head and tail will point to node next to temp and delete the middle node by setting the temp to null.
5. display() will display the nodes present in the list:
1. Define a node current which will initially point to the head of the list.
2. Traverse through the list till current points to null.
3. Display each node by making current to point to node next to it in each iteration.

### Python

```#Represent a node of the singly linked list
class Node:
def __init__(self,data):
self.data = data;
self.next = None;

class DeleteMid:
def __init__(self):
self.tail = None;
self.size = 0;

#Create a new node
newNode = Node(data);

#Checks if the list is empty
#If list is empty, both head and tail will point to new node
self.tail = newNode;
else:
#newNode will be added after tail such that tail's next will point to newNode
self.tail.next = newNode;
#newNode will become new tail of the list
self.tail = newNode;
#Size will count the number of nodes present in the list
self.size = self.size + 1;

#deleteFromMid() will delete a node from the middle of the list
def deleteFromMid(self):
#Checks if list is empty
print("List is empty");
return;
else:
#Store the mid position of the list
count = (self.size//2) if(self.size % 2 == 0)  else((self.size+1)//2);

#Checks whether head is equal to tail or not, if yes then list has only one node.
#Initially, temp will point to head
current = None;

#Current will point to node previous to
#If temp is pointing to node 2 then current will point to node 1.
for i in range(0, count-1):
current = temp;
temp = temp.next;

if(current != None):
#temp is the middle that needs to be removed.
#So, current node will point to node next to temp by skipping temp.
current.next = temp.next;
#Delete temp
temp = None;
#If current points to None then, head and tail will point to node next to temp.
else:
#Delete temp
temp = None;
#If the list contains only one element
#then it will remove it and both head and tail will point to None
else:
self.size = self.size - 1;

#display() will display all the nodes present in the list
def display(self):
#Node current will point to head
print("List is empty");
return;
while(current != None):
#Prints each node by incrementing pointer
print(current.data),
current = current.next;

sList = DeleteMid();

#Printing original list
print("Original List: ");
sList.display();

sList.deleteFromMid();
#Printing updated list
print("Updated List: ");
sList.display();
```

Output:

```Original List:
1 2 3 4
Updated List:
1 3 4
Updated List:
1 4
Updated List:
4
Updated List:
List is empty
```

### C

```#include <stdio.h>

//Represent a node of the singly linked list
struct node{
int data;
struct node *next;
};

int size;
struct node *head, *tail = NULL;

//Create a new node
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
newNode->next = NULL;

//Checks if the list is empty
//If list is empty, both head and tail will point to new node
tail = newNode;
}
else {
//newNode will be added after tail such that tail's next will point to newNode
tail->next = newNode;
//newNode will become new tail of the list
tail = newNode;
}
//Size will count the number of nodes present in the list
size++;
}

//deleteFromMid() will delete a node from the middle of the list
void deleteFromMid() {
struct node *temp, *current;

//Checks if list is empty
printf("List is empty \n");
return;
}
else {
//Store the mid position of the list
int count = (size % 2 == 0) ? (size/2) : ((size+1)/2);

//Checks whether the head is equal to the tail or not, if yes then the list has only one node.
if( head != tail ) {
//Initially, temp will point to head
current = NULL;

//Current will point to node previous to temp
//If temp is pointing to node 2 then current will point to node 1.
for(int i = 0; i < count-1; i++){
current = temp;
temp = temp->next;
}
if(current != NULL) {
//temp is the middle that needs to be removed.
//So, current node will point to node next to temp by skipping temp.
current->next = temp->next;
//Delete temp
temp = NULL;
}
//If current points to NULL then, head and tail will point to node next to temp.
else {
//Delete temp
temp = NULL;
}
}
//If the list contains only one element
//then it will remove it and both head and tail will point to NULL
else {
}
}
size--;
}

//display() will display all the nodes present in the list
void display() {
//Node current will point to head
printf("List is empty\n");
return;
}
while(current != NULL) {
//Prints each node by incrementing pointer
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}

int main()
{

//Printing original list
printf("Original List: \n");
display();

deleteFromMid();
//Printing updated list
printf("Updated List: \n");
display();
}

return 0;
}
```

Output:

```Original List:
1 2 3 4
Updated List:
1 3 4
Updated List:
1 4
Updated List:
4
Updated List:
List is empty
```

### JAVA

```#include <stdio.h>

//Represent a node of the singly linked list
struct node{
int data;
struct node *next;
};

int size;
struct node *head, *tail = NULL;

//Create a new node
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
newNode->next = NULL;

//Checks if the list is empty
//If list is empty, both head and tail will point to new node
tail = newNode;
}
else {
//newNode will be added after tail such that tail's next will point to newNode
tail->next = newNode;
//newNode will become new tail of the list
tail = newNode;
}
//Size will count the number of nodes present in the list
size++;
}

//deleteFromMid() will delete a node from the middle of the list
void deleteFromMid() {
struct node *temp, *current;

//Checks if list is empty
printf("List is empty \n");
return;
}
else {
//Store the mid position of the list
int count = (size % 2 == 0) ? (size/2) : ((size+1)/2);

//Checks whether the head is equal to the tail or not, if yes then the list has only one node.
if( head != tail ) {
//Initially, temp will point to head
current = NULL;

//Current will point to node previous to temp
//If temp is pointing to node 2 then current will point to node 1.
for(int i = 0; i < count-1; i++){
current = temp;
temp = temp->next;
}
if(current != NULL) {
//temp is the middle that needs to be removed.
//So, current node will point to node next to temp by skipping temp.
current->next = temp->next;
//Delete temp
temp = NULL;
}
//If current points to NULL then, head and tail will point to node next to temp.
else {
//Delete temp
temp = NULL;
}
}
//If the list contains only one element
//then it will remove it and both head and tail will point to NULL
else {
}
}
size--;
}

//display() will display all the nodes present in the list
void display() {
//Node current will point to head
printf("List is empty\n");
return;
}
while(current != NULL) {
//Prints each node by incrementing pointer
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}

int main()
{

//Printing original list
printf("Original List: \n");
display();

deleteFromMid();
//Printing updated list
printf("Updated List: \n");
display();
}

return 0;
}
```

Output:

```Original List:
1 2 3 4
Updated List:
1 3 4
Updated List:
1 4
Updated List:
4
Updated List:
List is empty
```

### C#

```using System;

public class CreateList
{
//Represent a node of the singly linked list
public class Node<T>{
public T data;
public Node<T> next;

public Node(T value) {
data = value;
next = null;
}
}

public class DeleteMid<T>{
public int size;
public Node<T> tail = null;

//Create a new node
Node<T> newNode = new Node<T>(data);

//Checks if the list is empty
//If list is empty, both head and tail will point to new node
tail = newNode;
}
else {
//newNode will be added after tail such that tail's next will point to newNode
tail.next = newNode;
//newNode will become new tail of the list
tail = newNode;
}
//Size will count the number of nodes present in the list
size++;
}

//deleteFromMid() will delete a node from the middle of the list
public void deleteFromMid() {
Node<T> temp, current;

//Checks if list is empty
Console.WriteLine("List is empty");
return;
}
else {
//Store the mid position of the list
int count = (size % 2 == 0) ? (size/2) : ((size+1)/2);

//Checks whether the head is equal to the tail or not, if yes then the list has only one node.
if( head != tail ) {
//Initially, temp will point to head
current = null;

//Current will point to node previous to temp
//If temp is pointing to node 2 then current will point to node 1.
for(int i = 0; i < count-1; i++){
current = temp;
temp = temp.next;
}
if(current != null) {
//temp is the middle that needs to be removed.
//So, current node will point to node next to temp by skipping temp.
current.next = temp.next;
//Delete temp
temp = null;
}
//If current points to null then, head and tail will point to node next to temp.
else {
//Delete temp
temp = null;
}
}
//If the list contains only one element
//then it will remove it and both head and tail will point to null
else {
}
}
size--;
}

//display() will display all the nodes present in the list
public void display() {
//Node current will point to head
Console.WriteLine("List is empty");
return;
}
while(current != null) {
//Prints each node by incrementing pointer
Console.Write(current.data + " ");
current = current.next;
}
Console.WriteLine();
}
}

public static void Main()
{
DeleteMid<int> sList = new DeleteMid<int>();

//Printing original list
Console.WriteLine("Original List: ");
sList.display();

sList.deleteFromMid();
//Printing updated list
Console.WriteLine("Updated List: ");
sList.display();
}
}
}
```

Output:

```Original List:
1 2 3 4
Updated List:
1 3 4
Updated List:
1 4
Updated List:
4
Updated List:
List is empty
```

### PHP

```<!DOCTYPE html>
<html>
<body>
<?php
//Represent a node of singly linked list
class Node{
public \$data;
public \$next;

function __construct(\$data){
\$this->data = \$data;
\$this->next = NULL;
}
}
class DeleteMid{
public \$tail;
function __construct(){
\$this->tail = NULL;
\$this->size = 0;
}

//Create a new node
\$newNode = new Node(\$data);

//Checks if the list is empty
//If list is empty, both head and tail will point to new node
\$this->tail = \$newNode;
}
else {
//newNode will be added after tail such that tail's next will point to newNode
\$this->tail->next = \$newNode;
//newNode will become new tail of the list
\$this->tail = \$newNode;
}
//Size will count the number of nodes present in the list
\$this->size++;
}

//deleteFromMid() will delete a node from the middle of the list
function deleteFromMid() {

//Checks if list is empty
print("List is empty <br>");
return;
}
else {
//Store the mid position of the list
\$count = (\$this->size % 2 == 0) ? (\$this->size/2) : ((\$this->size+1)/2);

//Checks whether head is equal to tail or not, if yes then list has only one node.
if( \$this->head != \$this->tail ) {
//Initially, temp will point to head
\$current = null;

//Current will point to node previous to temp
//If temp is pointing to node 2 then current will point to node 1.
for(\$i = 0; \$i < \$count-1; \$i++){
\$current = \$temp;
\$temp = \$temp->next;
}
if(\$current != null) {
//temp is the middle that needs to be removed.
//So, current node will point to node next to temp by skipping temp.
\$current->next = \$temp->next;
//Delete temp
\$temp = null;
}
//If current points to null then, head and tail will point to node next to temp.
else {
//Delete temp
\$temp = null;
}
}
//If the list contains only one element
//then it will remove it and both head and tail will point to null
else {
}
}
\$this->size--;
}

//display() will display all the nodes present in the list
function display() {
//Node current will point to head

print("List is empty <br>");
return;
}
while(\$current != NULL) {
//Prints each node by incrementing pointer
print(\$current->data . " ");
\$current = \$current->next;
}
print("<br>");
}
}

\$sList = new DeleteMid();

//Printing original list
print("Original List: <br>");
\$sList->display();

\$sList->deleteFromMid();
//Printing updated list
print("Updated List: <br>");
\$sList->display();
}
?>
</body>
</html>
```

Output:

```Original List:
1 2 3 4
Updated List:
1 3 4
Updated List:
1 4
Updated List:
4
Updated List:
List is empty
```

Next Topic#