0
0
DSA Pythonprogramming~30 mins

Delete Node by Value in DSA Python - Build from Scratch

Choose your learning style9 modes available
Delete Node by Value in a Singly Linked List
📖 Scenario: You are managing a list of tasks in a to-do app. Each task is stored as a node in a singly linked list. Sometimes, you need to remove a task by its name when it is completed or no longer needed.
🎯 Goal: Build a program that creates a singly linked list of tasks, sets the task name to delete, deletes the node with that task name from the list, and prints the updated list.
📋 What You'll Learn
Create a singly linked list with exact tasks: 'Laundry', 'Grocery', 'Homework', 'Exercise', 'Call Mom'
Create a variable called task_to_delete with the value 'Homework'
Write a function delete_node_by_value(head, value) that deletes the first node with the given value
Print the linked list after deletion in the format: Task1 -> Task2 -> ... -> null
💡 Why This Matters
🌍 Real World
Linked lists are used in many apps to manage ordered data like tasks, playlists, or history where items can be added or removed dynamically.
💼 Career
Understanding linked list operations like deletion is important for software development roles that involve data structure manipulation and memory management.
Progress0 / 4 steps
1
Create the singly linked list with tasks
Create a class called Node with attributes data and next. Then create nodes for tasks 'Laundry', 'Grocery', 'Homework', 'Exercise', and 'Call Mom' linked in that order. Assign the head of the list to a variable called head.
DSA Python
Hint

Start by defining the Node class with data and next. Then link the nodes in order.

2
Set the task to delete
Create a variable called task_to_delete and set it to the string 'Homework'.
DSA Python
Hint

Just assign the string 'Homework' to the variable task_to_delete.

3
Write the function to delete a node by value
Write a function called delete_node_by_value(head, value) that deletes the first node in the linked list whose data matches value. The function should return the new head of the list. Handle the case where the node to delete is the head.
DSA Python
Hint

Check if the head node has the value. If yes, return head.next. Otherwise, loop through nodes to find and remove the matching node.

4
Delete the node and print the updated list
Call delete_node_by_value(head, task_to_delete) and assign the result back to head. Then print the linked list nodes' data separated by ' -> ' and ending with 'null'.
DSA Python
Hint

Call the delete function and then loop through the list to print each task followed by ' -> ', ending with 'null'.