HomeAlgorithmsLinked List Deletion - Linked List Tutorial Series #3

Linked List Deletion – Linked List Tutorial Series #3

-

In this article, we will discuss about linked list deletion. We will see how to delete a node in a Singly linked list.

This article will be continuation of our Linked List tutorial series. You can navigate to other parts from here:

Agenda of this article

By the end of this article, you shall be able to delete a node at any position of a linked list.

Linked List Deletion

We may be asked to delete a node in a linked list at three different places:

  1. delete the first node
  2. delete the last node
  3. delete the node at a given position

Let us discuss one-by-one along with the coding examples.

1. Delete the first node
  1. Create a temp pointer that points to the same node as Head.
  2. Move head pointer to the next node and dispose the temp.

In python the temp will be garbage collected when it doesn’t have any reference.

Here’s the code to delete a node at start:

2. Delete the Last Node
  1. Traverse the list while maintaining previous node’s address also.
  2. After reaching the end of the list we will have two pointers -> one pointing to tail node, other pointing to the node before tail node.
  3. Update previous node’s next pointer to null
  4. Dispose the tail node.

Here’s the code to delete a node at end:

3. Deleting a node at a given position
  1. Maintain previous node and the node to be deleted.
  2. Once we find the node to be deleted, point the previous node’s pointer to the next pointer of the node to be deleted.
  3. Dispose the current node to be deleted.

Here’s the code to delete a node at a given position:

Now let us see the executable code of deletion of Linked list where we can call all the three functions discussed above.

Time Complexity

Time complexity for deleting a Node is O(n) where n is the length of linked list, as worst case might be last of the node to be deleted and we need to traversing the entire list.

Space Complexity

Space complexity – O(1) for a temp variable.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

LATEST POSTS

Best Time to Buy and Sell Stock – Day 7 | 100 Days of Code

Welcome to Day 7 of 100 Days of Code where today we solve yet another most frequently asked easy array problem called - Best Time...

Contains Duplicate – Day 8 | 100 Days of Code

Welcome to Day 8 of 100 Days of Code and today we are going to solve yet another easy-level problem - Contains Duplicate as part...

Two Sum – Day 6 | 100 Days of Code

Welcome to Day 6 of 100 Days of Code where we solve the most frequently asked Easy level Array problem - Two Sum. Let us...

Arrays for Beginners: Unveiling the Power of Data Structures

Hello, Programmers 🌟 Welcome to Coderefer! Let's explore the world of arrays together. Arrays are like handy data structures that help you manage and work...
Exit mobile version