-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverseIndex.py
More file actions
54 lines (41 loc) · 887 Bytes
/
reverseIndex.py
File metadata and controls
54 lines (41 loc) · 887 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class LinkedLis:
head = None
valueInNIndex = None
def __init__(self, head):
self.head = head
def lastN(self, n, node):
if node == None:
return 0;
reverseIndex = self.lastN(n, node.next) + 1
if reverseIndex == n:
self.valueInNIndex = node.value
return reverseIndex
def findLastN(self, n):
self.valueInNIndex = None
self.lastN(n, self.head)
def printLastN(self):
if self.valueInNIndex == None:
print("not found")
else:
print("value %s"%(self.valueInNIndex))
class Node:
next = None
reverNValue = None
def __init__(self, value):
self.value = value
def __repr__(self):
return str(self.value)
seis = Node(6)
uno = Node(1)
siete = Node(7)
tres = Node(3)
seis.next = uno
uno.next = siete
siete.next = tres
list = LinkedLis(seis)
list.findLastN(5)
list.printLastN()
list.findLastN(1)
list.printLastN()
list.findLastN(3)
list.printLastN()