DEV Community

Cover image for Linked List
Lancelot03
Lancelot03

Posted on • Edited on

Linked List

Linked List Creation--

class Node:   # Node Creation
    def __init__(self, data):
        self.data=data
        self.ref=None 
class LinkedList:   # Connecting the nodes
    def __int__(self):
        self.head=None
    def print_ll(self):  # Printing of the created linhed list
        if self.head is None:
            print("Linked list is empty")
            return
        n=self.head
        while n:
            print(n.data)
            n=n.ref

node1 =Node(10)   # Given the vazlue in the 1st node
node2=Node(2)   # Giving the value in the second node
q1=LinkedList()  # Calling the Linkeglist function
q1.head=node1  # pointing of q1 as a head node
node1.ref=node2   # storing the addresss of the 2nd node into 
                    first node
q1.print_ll()  # printing of q1
Enter fullscreen mode Exit fullscreen mode

Top comments (0)