Skip to content
Programming101
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programmingoneonone

Learn everything about programming

Deletion in Linked list | Data structures

YASH PAL, 12 May 202028 May 2024

Deletion – Linked list as we know for performing operations on the linked list we need to find the reference to the node of the list. so to delete a node from any position in the linked list we need to first find the reference of the node.

and if you don’t know how to find the reference of a node in the linked list then first complete read this post.

  • Operations on a linked list

Deletion – Linked list in Data structures and algorithms

  1. Deletion of the first node
  2. Deletion of the only node
  3. Deletion in between the node
  4. Deletion at the end of the list

Deletion of the first node from the linked list.

as you see we have a linked list that contains 5 items in it.

Deletion in Linked list Data structures

to delete the first node of the list we need to point the start variable of the linked list to the second node of the list.

Deletion in Linked list Data structures

so now the first node of the linked list is deleted.

Deletion in Linked list Data structures
def delete_first_node(self):
    if self.start is None:
        return    self.start = self.start.link

Deletion of the only node from the linked list.

like if we have a list that has only one node.

Deletion in Linked list Data structures

then to delete the only node of the list we store the null or None value to the start variable.

Deletion in Linked list Data structures

so now our list becomes empty.

Deletion in Linked list Data structures
self.start = None

Deletion in between the nodes of a linked list

To delete a node between the nodes of the linked list first we need to find the reference of the predecessor of the node that we want to delete. like in the example given below we want to delete the second and fourth nodes. so we want to delete the node that contains the value 30.

so we need to find the reference to the node that comes before the node that contains the value 30.

Deletion in Linked list Data structures

then we refer the p node to the node that comes second after the node p. as you see in the image given below.

Deletion in Linked list Data structures

now the node is deleted from the list.

Deletion in Linked list Data structures

Deletion at the end of the linked list.

to delete the last node we need a reference to the second last node of the linked list. as we see in the image given below.

Deletion in Linked list Data structures

after that, we set the linked part of p to null or None.

Deletion in Linked list Data structures

so now the last node of the linked list is deleted.

Deletion in Linked list Data structures
def delete_last_node(self):

    if self.start is None:
        return
    if self.start.link is None:
        self.start = None        return
    p = self.start
    while p.link.link is not None:
        p = p.link
    p.link = None
Computer Science Tutorials Data Structures Tutorials computer scienceData Structure

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes
Programming101
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions