0
0
DSA Pythonprogramming~30 mins

Delete from Beginning of Doubly Linked List in DSA Python - Build from Scratch

Choose your learning style9 modes available
Delete from Beginning of Doubly Linked List
📖 Scenario: You are managing a playlist of songs where each song is linked to the previous and next song. Sometimes, you want to remove the first song from the playlist.
🎯 Goal: Build a doubly linked list with three songs, then delete the first song from the list, and finally print the updated playlist.
📋 What You'll Learn
Create a doubly linked list with three nodes containing the exact song names: 'Song1', 'Song2', 'Song3'.
Create a variable called head pointing to the first node.
Write a function called delete_from_beginning that removes the first node from the doubly linked list.
Print the updated list starting from head after deletion.
💡 Why This Matters
🌍 Real World
Doubly linked lists are used in music players, web browsers (back and forward navigation), and undo-redo features where you need to move both forward and backward.
💼 Career
Understanding how to manipulate doubly linked lists is important for software engineers working on systems that require efficient insertions and deletions from both ends, such as caches and navigation history.
Progress0 / 4 steps
1
Create the Doubly Linked List with Three Songs
Create a class called Node with attributes data, prev, and next. Then create three nodes with data 'Song1', 'Song2', and 'Song3'. Link them to form a doubly linked list and set head to the first node.
DSA Python
Hint

Remember to link nodes both ways: next and prev.

2
Create a Function to Delete from Beginning
Create a function called delete_from_beginning that takes head as input and returns the new head after deleting the first node. If the list is empty, return None. If there is a next node, update its prev to None.
DSA Python
Hint

Check if head is None first. Then update new_head.prev to None if it exists.

3
Delete the First Node from the List
Call the function delete_from_beginning with the variable head and update head with the returned new head.
DSA Python
Hint

Assign the result of delete_from_beginning(head) back to head.

4
Print the Updated Doubly Linked List
Use a while loop with a variable current starting at head to print the data of each node separated by -> . End the print with null.
DSA Python
Hint

Use a loop to print each node's data followed by -> , then print null at the end.