I'm trying to create a prepend method that takes the value and insert it between the head and the next value in the linked list, and here is where I am at the moment, it only prints none < because of the print statement and then it prints just 1:
Код: Выделить всё
class Node: def __init__(self, value): self.value = value self.next = None class LinkedList: def __init__(self): self.head = None def append(self, value): if self.head is None: self.head = Node(value) return # Move to the tail (the last node) node = self.head while node.next: node = node.next node.next = Node(value) return def prepend(self,value): if self.head is None: self.head = Node(value) return else: holder = None self.head.next = holder #holder.next = holder print(holder) return linked_list = LinkedList() linked_list.append(1) linked_list.append(2) linked_list.append(4) linked_list.prepend(5) node = linked_list.head while node: print(node.value) node = node.next Источник: https://stackoverflow.com/questions/609 ... inked-list