![]() ![]() Raise Inde圎rror("Positive index out of range")Ĭan someone please help explain this?Any help will be appreciated thanks!ĮDIT:Okay apparently it inserts the last item first?Why is that?ġ -When you call insert with index equal to zero you insert the list item upfront, that is, in the location pointed by head(see method def insert(self, index, item):Ģ - When the method copy is called the method def _copy_aux_(self, node, new_list): is called recursively until the last item of the list is reached, which has node.next equals to None.ģ - After that, after each return from the method _copy_aux_, it starts inserting the itens in the new_list up front from the last to the first item, which gives the correct order. However changing what it points to (via l newnode ) only changes what the pointer inside the function points to, it does not affect the one you passed as a parameter. ![]() ![]() My insert function def insert(self, index, item): def remove (self, val): '''removed the node containing val from the linked list - helper function''' if self.head and val: self.head return self.recursiveremove (self.head, val) if its at the start, the head needs to be changed. Having node l as a parameter means that your function has a local pointer variable that points to the same address as the pointer you pass in. recurse through the list once to find the list length, then write a recursive method to return the k th element (a much easier problem). Note this defines a list using a smaller list. We need to reverse the list by changing links between nodes. A linked list is itself a recursive structure: A list is either empty or it's an element linked by its 'next' reference to a list. Then wouldn't the copied list be reversed? But I tried running it and the output is correct and is in order? Given pointer to the head node of a linked list, the task is to recursively reverse the linked list. Hi I implemented a linked list in Python and I got this piece of code that can copy the linked list to another linked list but I cannot understand why its inserting at 0 index instead? Stack is a linear data structure following LIFO (Last in First out) order and can be implemented using Array or Linked List as an internal data structure. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |