-
Notifications
You must be signed in to change notification settings - Fork 111
/
solution.py
25 lines (22 loc) · 839 Bytes
/
solution.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
class SinglyLinkedListNode: #class SinglyLinkedListNode has a data variable and a pointer to the next node
def __init__(self, data):
self.data = data
self.next = None
def print_with_head(head) : #helper functioin to print the elements of the list
temp = head
while temp is not None :
print(temp.data)
temp = temp.next
def insert_at_head(head, data):
temp = SinglyLinkedListNode(data) # create a new node for the new head
temp.next = head # assign new head's next as previous head
head = temp
return head
if __name__ == '__main__' : # to test implementation
print("Enter the size of the linked list : ")
n = int(input())
print("Enter list elements")
head = None
for i in range(n):
head = insert_at_head(head, int(input()))
print_with_head(head)