From e4d78b183a9828807f872b475396576e1260284e Mon Sep 17 00:00:00 2001 From: Amal Ragh K <78066884+AmalRaghk@users.noreply.github.com> Date: Sat, 8 Oct 2022 21:36:19 +0530 Subject: [PATCH] Create linkedlisttraversal.py --- linkedlisttraversal.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 linkedlisttraversal.py diff --git a/linkedlisttraversal.py b/linkedlisttraversal.py new file mode 100644 index 0000000..6c97d3a --- /dev/null +++ b/linkedlisttraversal.py @@ -0,0 +1,39 @@ +# Node class +class Node: + + # Function to initialise the node object + def __init__(self, data): + self.data = data # Assign data + self.next = None # Initialize next as null + + +# Linked List class contains a Node object +class LinkedList: + + # Function to initialize head + def __init__(self): + self.head = None + + # This function prints contents of linked list + # starting from head + def printList(self): + temp = self.head + while (temp): + print (temp.data) + temp = temp.next + + +# Code execution starts here +if __name__=='__main__': + + # Start with the empty list + llist = LinkedList() + + llist.head = Node(1) + second = Node(2) + third = Node(3) + + llist.head.next = second; # Link first node with second + second.next = third; # Link second node with the third node + + llist.printList()