Post

Day 23 of 100 Days of Python

Practising DSA

Practising Linked List

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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")
    def append(self, data):
        new_node = Node(data)
        if self.head is None:
            self.head = new_node
            return
        
        current = self.head
        while current.next is not None:
            current = current.next
        current.next = new_node
ll = LinkedList()
ll.append(10)
ll.append(20)
ll.prepend(5)
ll.append(30)
ll.display()

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

Output:

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

Trending Tags