Program for n’th node from the end of a Linked List
Given a Linked List and a number n, write a function that returns the value at the n’th node from end of the Linked List.
class SingleLinkedList(object):
def __init__(self,val):
self.value = val
self.next = None
def __repr__(self):
return str(self.value)
class Solution(object):
def find_nth_node_from_last(self, head,n):
slow_ptr = head
fast_ptr = head
for _ in range(n):
if not fast_ptr:
return None
fast_ptr = fast_ptr.next
print fast_ptr
while fast_ptr:
fast_ptr = fast_ptr.next
slow_ptr = slow_ptr.next
return slow_ptr