This repository was archived by the owner on Apr 29, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquestion5.py
More file actions
62 lines (49 loc) · 1.94 KB
/
question5.py
File metadata and controls
62 lines (49 loc) · 1.94 KB
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
#!/usr/bin/python3
from my_helpers import myPrint, \
headerPrint, \
subHeaderPrint # Some simple text rendering functions
#------------------------------ Question #5 ------------------------------------
# Udacity text:
# Find the element in a singly linked list that's m elements from the end.
# For example, if a linked list has 5 elements, the 3rd element from the end
# is the 3rd element. The function definition should look like
# question5(ll, m), where ll is the first node of a linked list and m is the
# "mth number from the end". You should copy/paste the Node class below to
# use as a representation of a node in the linked list. Return the value of
# the node at that position.
class Node(object):
def __init__(self, data):
self.data = data
self.next = None
# Create a linked list
ll = Node(1)
ll.next = Node(2)
ll.next.next = Node(3)
ll.next.next.next = Node(4)
ll.next.next.next.next = Node(5)
def print_ll(ll):
""" Prints out the linked list. """
node = ll
while node != None:
print(node.data)
node = node.next
def getFromHead(ll, m):
node = ll
# For second element `m = 2`, we run this loop once.
# If iteration returns `None`, abort query and return `None`
for iter in range(1, m):
node = node.next
if node == None:
return None
# Node is now the mth element from the start of the linked list
return node.data
def question5(ll, m):
return getFromHead(ll, m)
headerPrint('Question 5 - Specific element location in singly linked list')
subHeaderPrint('Printing generated linked list with 5 nodes:')
print_ll(ll)
myPrint('')
subHeaderPrint('Printing value of 3rd element of a linked list with 5 elements')
myPrint(question5(ll, 3))
print('')
input('Press the enter key to continue...')