def insert(self, cargo): node = Node(cargo) if self.length == 0: self.head = self.last = node else: last = self.last last.next = node # Add the node to the end of the queue self.last = node # Identify the last node as the last node in the queue self.length += 1
def setUp(self) -> None: self.linkedlist = LinkedList() node1 = Node() node2 = Node() node1.next = node2 node3 = Node() node2.next = node3 self.linkedlist.head = node1
def insert(self, cargo): node = Node(cargo) if self.head is None: self.head = node # If the queue is empty, the new node goes first else: last = self.head while last.next: last = last.next # Find the last node in the queue last.next = node # Add the new node to the end of the queue self.length += 1
def insert(self, cargo): first = self.head if first is None: node = Node(cargo) self.head = node return if first.cargo < cargo: node = Node(cargo) node.next = first self.head = node return while first.next is not None: if first.next.cargo < cargo: break first = first.next node = Node(cargo) node.next = first.next first.next = node return
def insert(self, key, val): ind = self.hash(key) node = Node(val=(key, val)) self.table[ind].insert_at_end(node)
if node.data < x: if low_tail: low_tail.next = node else: first_low = node low_tail = node else: if high_tail: high_tail.next = node else: first_high = node high_tail = node node = next low_tail.next = first_high return LinkedList(first_low) last = Node(data=1) c = Node(2, next=last) a = Node(10, next=c) b = Node(5, next=a) first = Node(8, next=b) ll = LinkedList(first) debug(ll) print('partition', debug(partition(ll, 5)))
def push(self, data): head = Node(data) head.next = self.head self.head = head
from linked_lists import LinkedList, Node, debug def remove_dups(linked_list): seen = {} last_node = None node = linked_list.head while node != None: if node.data in seen: last_node.next = node.next else: seen[node.data] = True last_node = node node = node.next return linked_list last = Node(data='a') c = Node('c', next=last) a = Node('a', next=c) b = Node('b', next=a) first = Node('a', next=b) ll = LinkedList(first) debug(ll) remove_dups(ll) debug(ll)
def test_insert_at_position(self): node_pos = Node() self.linkedlist.insert_at_position(0, node_pos) self.assertEqual(4, self.linkedlist.len())
def test_insert_at_end(self): node_end = Node() self.linkedlist.insert_at_end(node_end) self.assertEqual(4, self.linkedlist.len())
from linked_lists import Node, LinkedList lst = [2, 3, 4, 5, 6] if "__main__" == __name__: llst = LinkedList() node1 = Node() node1.data = 1 node2 = Node() node2.data = 2 node1.next = node2 node3 = Node() node3.data = 3 node2.next = node3 llst.head = node1 #inserting the value at the begining. node = Node() node.data = 0 llst.insert_at_begining(node) #inserting the value at the end node4 = Node() node4.data = 4 llst.insert_at_end(node4)