Post

Day 22 of 100 Days of Python

Practising DSA

Today i practised Linked List while using a separate class for linking Nodes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None
class LinkedList:
    def __init__(self):
        self.head =None
    def prepend(self,data):
        new_node = Node(data)
        new_node.next = self.head
        self.head = new_node
    def display(self):
        current = self.head
        while current is not None:
            print(current.data, end=" -> ")
            current = current.next
        print("None")
ll = LinkedList()
ll.prepend(30)
ll.prepend(20)
ll.prepend(10)
ll.display()

# Expected Output: 10 -> 20 -> 30 -> None

Output:

1
10 -> 20 -> 30 -> None
This post is licensed under CC BY 4.0 by the author.

Trending Tags