forked from nathan-abela/HackerRank-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16 - Day 15 - Linked List.py
47 lines (38 loc) · 1.01 KB
/
16 - Day 15 - Linked List.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
# ========================
# Information
# ========================
# Direct Link: https://www.hackerrank.com/challenges/30-linked-list/problem
# Difficulty: Easy
# Max Score: 30
# Language: Python
# ========================
# Solution
# ========================
class Node:
def __init__(self, data):
self.data = data
self.next = None
class Solution:
def display(self, head):
current = head
while current:
print(current.data, end=' ')
current = current.next
def insert(self, head, data):
# For each node except the first node
if type(head) == Node:
new_node = head
while new_node.next:
new_node = new_node.next
new_node.next = Node(data)
#First node
else:
head = Node(data)
return head
mylist = Solution()
T = int(input())
head = None
for i in range(T):
data = int(input())
head = mylist.insert(head, data)
mylist.display(head)