Insert node in specific index of linked list Python

0

Good, I have a doubt with this that I can not think of how to do it, as the title says, I have a typical node and a typical list

class node:
    def __init__(self, info):
        self.info = info
        self.next = None

class list:
    def __init__(self):
        self.__first = None
        self.__last = None

and I have to create the function that inserts the node in the specific position of the given list (I put here what I have) (I do not want to change something of the node, nor create methods)

def indexar(self, info, indice):
    longit = self.tam()
    n = node(info)
    p = self.__first
    if index == 0:
        self.__first = n
    else:
        if index > 0 and index < longit:

already create the function self.tam () that delivers the size of the list, and I already have in case you want to enter the position 0, but already for a non-initial X position I can not achieve it (rather, imagine it), any kind of help or recommendation is eternally grateful

    
asked by Esteban 16.04.2017 в 00:53
source

1 answer

-1

You have to go through the list and when you find the right place you must reassign the next pointer of the previous node to point to your new node and the next pointer of the new node must point to the node that follows in the list

    
answered by 16.04.2017 в 18:00