python
linked list
5 years, 8 months ago
class Node(object):
def __init__(self, data=None, next_node=None):
self.data = data
self.next_node = next_node
def set_next(self, new_next):
self.next_node = new_next
class LinkedList(object):
def __init__(self, head=None):
self.head = head
def insert(self, data):
new_node = Node(data)
new_node.set_next(self.head)
self.head = new_node
l = LinkedList()
data = [1,2,3,4,5]
for number in data:
l.insert(number)
print (l.head.data) # it would print 5
# Iterate or Print all elements in linked list
tmp = l.head
while(tmp):
print (tmp.data)
tmp = tmp.next_node
0 Comments
Please Login to Comment Here