-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquestion5.py
More file actions
39 lines (31 loc) · 733 Bytes
/
question5.py
File metadata and controls
39 lines (31 loc) · 733 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
class Node(object):
def __init__(self, data):
self.data = data
self.next = None
def question5(ll, m):
# Setting up initial values - head of linked list
p1 = ll
p2 = ll
# This cycle sets p1 m positions in front of p2
for i in range(0, m):
if (p1 == None):
return None
p1 = p1.next
# This while loop will run until p1 get to the last element of list
# at the time p2 will be m positions behin
# p2 value is returned, since that with be the mth element
# in the linked list
while (p1 != None):
p1 = p1.next
p2 = p2.next
return p2.data
A = Node(2)
B = Node(3)
C = Node(8)
D = Node(1)
E = Node(6)
A.next = B
B.next = C
C.next = D
D.next = E
print(question5(A, 3))