Linked List Nth to Last Node | Python

Problem :

Function that takes a head node and an integer value n and 
then returns the nth to last node in the linked list.

Solution : 

class Node:

    def __init__(self, value):
        self.value = value
        self.nextnode  = None

    def nth_to_last_node(n, head):

	node = head

	nodes = []
	while node != None:
		nodes.append(node)
		node = node.nextnode

	return nodes[-n]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s