def prepend(self, data):
     new_node = Node(data)
     if self.head is None:
         self.head = new_node
         return
     new_node.next = self.head
     self.head = new_node
Пример #2
0
 def append(self, data):
     new_node = Node(data)
     if not self.head:
         self.head = new_node
         new_node.next = new_node
     else:
         curr = self.head
         while curr.next != self.head:
             curr = curr.next
         curr.next = new_node
         new_node.next = self.head
Пример #3
0
 def append(self, val: int):
     """
     adds a new node to the linked list at the end of the list
     """
     if self.head == None:
         self.head = Node(val)
     else:
         current = self.head
         while current.next_ != None:
             current = current.next_
         current.next_ = Node(val)
     self.length += 1
    def append(self, data):
        new_node = Node(data)
        current_node = self.head

        while current_node.next is not None:
            current_node = current_node.next
        current_node.next = new_node
Пример #5
0
    def insert(self, val: int):
        """
        insert takes an integetr
        """

        newNode = Node(val, self.head)
        self.head = newNode
        self.length += 1
    def append(self, data):
        new_node = Node(data)

        if self.head is None:
            self.head = new_node
            return

        last_node = self.head
        while last_node.next:
            last_node = last_node.next
        last_node.next = new_node
Пример #7
0
    def insert_before(self, val: int, newVal: int):
        """
        adds a new node to the ll before the 'val' node
        """
        current = self.head
        last = None
        if self.head.val == val:
            newNode = Node(newVal, self.head)
            self.head = newNode
            return

        while current != None:
            if current.val == val:
                newNode = Node(newVal, current)
                last.next_ = newNode
                return
            else:
                last = current
                current = current.next_
        raise KeyError
Пример #8
0
 def insert_after(self, val: int, newVal: int):
     """
     adds a new node to the ll after the 'val' node
     """
     current = self.head
     while current != None:
         if current.val == val:
             newNode = Node(newVal, current.next_)
             current.next_ = newNode
             self.length += 1
             return "Success"
         current = current.next_
     raise KeyError
 def insertAfter(self, previous, data):
     new_node = Node(data)
     new_node.next = previous.next
     previous.next = new_node
 def __init__(self):
     self.head = Node()
def test_LinkedList_takes_node():
    node1 = Node(1)
    ll1 = LinkedList()
    assert 1 == 1
def test_Node_doesnt_hold_val():
    node2 = Node(2)
    assert node2.val != node2
def test_Node_holds_val():
    node1 = Node(1)
    assert node1.val == 1
def test_Node_class_exists():
    assert Node(5)