0
0
DSA Pythonprogramming~30 mins

Delete Node at Beginning in DSA Python - Build from Scratch

Choose your learning style9 modes available
Delete Node at Beginning in a Singly Linked List
📖 Scenario: Imagine you have a line of people waiting to buy tickets. Sometimes, the first person in line leaves. We want to update the line to show who is now first.
🎯 Goal: You will create a simple linked list, then remove the first person (node) from the line (list), and finally show the updated line.
📋 What You'll Learn
Create a singly linked list with exactly three nodes containing values 10, 20, and 30 in that order.
Create a variable called head that points to the first node.
Write code to delete the first node by updating head to the next node.
Print the linked list after deletion in the format: 20 -> 30 -> None.
💡 Why This Matters
🌍 Real World
Linked lists are used in real life to manage queues, playlists, or any ordered collection where items can be added or removed easily.
💼 Career
Understanding linked lists and how to manipulate them is important for software developers working with data structures, memory management, and efficient data processing.
Progress0 / 4 steps
1
Create a singly linked list with three nodes
Define a class called Node with an __init__ method that takes data and sets self.data and self.next. Then create three nodes with values 10, 20, and 30. Link them so the first node points to the second, and the second points to the third. Finally, create a variable called head that points to the first node.
DSA Python
Hint

Start by defining the Node class. Then create three nodes and link them by setting the next attribute.

2
Prepare to delete the first node
Create a variable called temp and set it equal to head. This will help us keep track of the node we want to delete.
DSA Python
Hint

Use temp = head to hold the first node before deleting it.

3
Delete the first node by updating head
Update the variable head to point to the next node after temp. This removes the first node from the list.
DSA Python
Hint

Set head = temp.next to remove the first node from the list.

4
Print the linked list after deletion
Use a while loop with a variable current starting at head. Print each node's data followed by ->. When the loop ends, print None.
DSA Python
Hint

Use a loop to print each node's data followed by '->'. End with printing 'None'.