Linked Lists - Get Nth

Implement a GetNth() function that takes a linked list and an integer index and returns the node stored at the Nth index position. GetNth() uses the C numbering convention that the first node is index 0, the second is index 1, ... and so on. So for the list 42 -> 13 -> 666, GetNth() with index 1 should return Node(13);

The index should be in the range [0..length-1]. If it is not, GetNth() should throw/raise an exception (`ArgumentException` in C#, `InvalidArgumentException` in PHP). You should also raise an exception (`ArgumentException` in C#, `InvalidArgumentException` in PHP) if the list is empty/null/None.

Prerequisite Kata (may be in beta):

- Linked Lists - Push & BuildOneTwoThree
- Linked Lists - Length & Count

> The push() and buildOneTwoThree() (`BuildOneTwoThree` in C#, `build_one_two_three()` in PHP) functions do not need to be redefined.

Source: codewars | Difficulty: EASY
Tags: Fundamentals

Data structures

Starter code: def __init__(self, data):
	

Solutions:
class Node(object):

	def __init__(self, data):
		self.data = data
		self.next = None

def get_nth(node, index):
	v = -1
	n = node
	while n:
		v += 1
		if v == index:
			return n
		n = n.next
	raise ValueError


class Node(object):

	def __init__(self, data):
		self.data = data
		self.next = None

def get_nth(node, index):
	if node and index >= 0:
		return node if index < 1 else get_nth(node.next, index - 1)
	raise ValueError


class Node(object):

	def __init__(self, data):
		self.data = data
		self.next = None

def get_nth(node, index, i=0):
	if node is None:
		raise IndexError
	else:
		return node if index == i else get_nth(node.__next__, index, i + 1)


class Node(object):

	def __init__(self, data):
		self.data = data
		self.next = None

def get_nth(node, index):
	if node is None or index < 0:
		raise Exception('Invalid parameters.')
	while index > 0:
		if node.__next__ is None:
			raise Exception('Null node encountered.')
		node = node.__next__
		index -= 1
	return node


class Node(object):

	def __init__(self, data):
		self.data = data
		self.next = None

def length(head):
	if not head:
		return 0
	else:
		length = 0
		current_node = head
		while current_node is not None:
			length += 1
			current_node = current_node.next
		return length

def get_nth(head, index):
	if not head:
		raise ValueError('Head node cannot be None')
	if index < 0:
		raise ValueError('Index cannot be negative')
	if index > length(head) - 1:
		raise ValueError('Index cannot exceed number of nodes in list')
	counter = 0
	current_node = head
	while counter != index:
		current_node = current_node.next
		counter += 1
	return current_node


class Node(object):

	def __init__(self, data):
		self.data = data
		self.next = None

def get_nth(node, index):
	if not node or index < 0:
		raise ValueError
	while index:
		node = node.__next__
		if not node:
			raise ValueError
		index -= 1
	return node


class Node(object):

	def __init__(self, data):
		self.data = data
		self.next = None

def get_nth(node, index):
	while index:
		node = node.__next__
		index -= 1
	return node if node else node.data


class Node(object):

	def __init__(self, data):
		self.data = data
		self.next = None

def get_nth(node, index):
	if node is None or index < 0:
		raise LookupError('invalid index')
	for i in range(index):
		if node.__next__ is None:
			raise IndexError('index out of range')
		node = node.__next__
	return node


class Node(object):

	def __init__(self, data, nxt=None):
		(self.data, self.next) = (data, nxt)

def get_nth(node, index):
	i = 0
	while i < index and node:
		(node, i) = (node.next, i + 1)
	if node and i == index:
		return node
	raise IndexError


class Node(object):

	def __init__(self, data):
		self.data = data
		self.next = None

def get_nth(node, index):
	assert node and index >= 0
	for _ in range(index):
		node = node.next
		assert node
	return node