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!

# Function to delete a Linked list

Last Updated on June 22, 2022 by Ria Pathak

### Introduction

The linked list is one of the most important data structures to learn while preparing for interviews. Having a good grasp of Linked Lists can be a huge plus point in a coding interview.

### Problem Statement

In this question, we are given a singly linked list. We have to write a function that takes the head of the list as a parameter and deletes the complete linked list.

### Problem Statement Understanding

Suppose the linked list is 1 – > 2 – > 3 – > 4. Now, we have to delete this linked list.

• We have to delete the nodes one by one, so in the first step after deletion of 1, the linked list will be 2 – > 3 – > 4.
• In the second step after deletion of 2, the linked list will be 3 – > 4.
• In the third step after deletion of 3, the linked list will be 4.
• In the final step, 4 will also get deleted, and we will have an empty list.

Input:

Output: NULL (Empty List)

This question is not a very complex one. Firstly, do we have to do the linked list deletion in Java and Python? The answer is No. We have to solve it only for C++, as in Java and Python, an automatic garbage collection process happens, so the deletion of a linked list is easy. In Java and Python, we just have to change the head to NULL.

Let us have a glance at the approach.

### Approach

The approach is going to be pretty simple. We are going to create two nodes, current and temp, current will point to the head and temp will be NULL. Now, we will traverse through the list until the current is not NULL. In every iteration, we will make temp point to the next of current. Then, we will free current and lastly, the temp will become the new current.

In the end, we will make the head of the list NULL.

### Algorithm

• Create two nodes, current and temp, current will point to the head and temp will be NULL.
• Traverse through the list using current, and for every iteration, do the following.
1) Store the next of current in temp.
2) free(current)
3) Make temp as the new current.
• In the end, make the head of the list NULL.

### Code Implementation

```#include
#include
#include

struct Node
{
int data;
struct Node* next;
};

{
struct Node* next;

while (current != NULL)
{
next = current->next;
free(current);
current = next;
}

}

void push(struct Node** head_ref, int new_data)
{
struct Node* new_node =
(struct Node*) malloc(sizeof(struct Node));

new_node->data  = new_data;

}

int main()
{

/* Use push() to construct below list
1->12->1->4->1  */

}

```
```
#include
using namespace std;

class Node {
public:
int data;
Node* next;
};

{

Node* temp = NULL;

while (current != NULL)
{
temp = current->next;
free(current);
current = temp;
}

}

{
Node* new_node = new Node();

new_node->data = new_data;

}

int main()
{

cout << "Deleting the linked list";

}
```
```class LinkedList
{
class Node
{
int data;
Node next;
Node(int d) { data = d; next = null; }
}

/* Function deletes the entire linked list */
void deleteList()
{
}

/* Inserts a new Node at front of the list. */
public void push(int new_data)
{
Node new_node = new Node(new_data);
}

public static void main(String [] args)
{

llist.push(1);
llist.push(4);
llist.push(1);
llist.push(9);
llist.push(1);

System.out.println("Deleting the list");
llist.deleteList();

}
}
```
```
class Node:

def __init__(self, data):
self.data = data
self.next = None

def __init__(self):

def deleteList(self):

while current:
prev = current.next
del current.data
current = prev

def push(self, new_data):

new_node = Node(new_data)

if __name__ == '__main__':

llist.push(4)
llist.push(3)
llist.push(2)
llist.push(1)

llist.deleteList()

```

Output