Skip to content

Commit 2507ff0

Browse files
authored
Update linkedList_newway.py
1 parent c29857b commit 2507ff0

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

linkedList_newway.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,22 @@ def printList(self):
4141
print(current.data)
4242
current = current.next
4343

44+
# Returns data at given index in linked list
45+
def getNth(self, index):
46+
current = self.head # Initialise temp
47+
count = 0 # Index of current node
48+
49+
# Loop while end of linked list is not reached
50+
while (current):
51+
if (count == index):
52+
return current.data
53+
count += 1
54+
current = current.next
55+
56+
# if we get to this line, the caller was asking
57+
# for a non-existent element so we assert fail
58+
assert(false)
59+
return 0;
4460

4561
n1 = Node(10)
4662
ll = LinkedList()

0 commit comments

Comments
 (0)