-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChallenge_1.py
More file actions
39 lines (30 loc) · 916 Bytes
/
Challenge_1.py
File metadata and controls
39 lines (30 loc) · 916 Bytes
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
from _LinkedList import LinkedList
from _Node import Node
# Access HeadNode => list.getHead()
# Check if list is empty => list.isEmpty()
# Node class { int data ; Node nextElement;}
# Inserts a value at the end of the list
def insert_at_tail(lst, value):
# Creating a new node
new_node = Node(value)
# Check if the list is empty, if it is simply point head to new node
if lst.get_head() is None:
lst.head_node = new_node
return
# if list not empty, traverse the list to the last node
temp = lst.get_head()
while temp.next_element:
temp = temp.next_element
# Set the nextElement of the previous node to new node
temp.next_element = new_node
return
lst = LinkedList()
lst.print_list()
insert_at_tail(lst, 0)
lst.print_list()
insert_at_tail(lst, 1)
lst.print_list()
insert_at_tail(lst, 2)
lst.print_list()
insert_at_tail(lst, 3)
lst.print_list()