-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday-15-solution.py
70 lines (50 loc) · 1.28 KB
/
day-15-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
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
class Node:
def __init__(self, data):
# Each node has `data` and `next` pointer placeholder
self.data = data
self.next = None
class MyList:
def display(self, head):
# Set current to point to head
current = head
# Traverse till last node
while current is not None:
# Print data
print(current.data, end=" ")
# Move to next node
current = current.next
def insert_at_tail(self, head, data):
if head != None:
# Non-empty linked list
# Set current to point to head
current = head
# Traverse till last node
while current.next != None:
current = current.next
# Create new node
new_node = Node(data)
# Point tails to the new node
current.next = new_node
else:
# Empty linked list
# Create new node
new_node = Node(data)
# Set head to new node
head = new_node
# Return head
return head
if __name__ == "__main__":
# Create an instance of custom list implementation
a_list = MyList()
# Read test case input from stdin
T = int(input())
# Set `head` to None
head = None
# Iterate over the range `T`
for i in range(T):
# Read input data from stdin to insert in the list
data = int(input())
# Insert data to list
head = a_list.insert_at_tail(head, data)
# Display the list content
a_list.display(head)