-
Notifications
You must be signed in to change notification settings - Fork 0
/
16.linked-list-class.py
52 lines (41 loc) · 1.14 KB
/
16.linked-list-class.py
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""
author: Dr. Mohammed Zia
https://www.linkedin.com/in/zia33
Problem Statement:
A linked list class
more: ...
"""
class Node:
"""
"""
def __init__(self, item = None, next = None):
self.item = item
# next is an internal method to link to another obj
self.next = next
return
class LinkedList:
"""
"""
def __init__(self):
self.head = None
def insert(self, item):
new_node = Node(item)
if(self.head):
current_node = self.head
# next method is being called to check if any child node exists or not. If not then assign newly provided node as terminating child.
while(current_node.next):
current_node = current_node.next
current_node.next = new_node
else:
self.head = new_node
def print_ll(self):
current_node = self.head
while(current_node):
print(current_node.item)
current_node = current_node.next
if __name__ == '__main__':
ll = LinkedList()
ll.insert(3)
ll.insert(4)
ll.insert(5)
ll.print_ll()