Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Node(): #First we will create the nodes which will contain 2 fields (i) data (ii) link/reference
- def __int__(self,data):#initializing the node with 2 fields (i) data (ii) link/reference
- self.data = data
- self.reference = None #for empty value / setting reference as None
- class LinkedList(): #connecting those nodes
- def __init__(self):
- self.head = None #initializing the head as None / which also means the list is empty or has no nodes in it
- #Note Reference of the first node is stored in the head
- def viweing_linkedlist(self): #Traversing The LinkedList
- if self.head is None:
- print("Linked list is empty!")
- else:
- n = self.head
- while n is not None:
- print(n.data)
- n = n.reference
- def add_begin(self, data):#Adding in the Linked List
- new_node = Node(data)
- new_node.reference = self.head
- self.head = new_node
- #For Reference Refer to
- #https://www.youtube.com/watch?v=xRTdfZsAz6Y
- #https://www.youtube.com/watch?v=B-zO18TJKYQ
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement