def sumLists(L1, L2): if not L1 and not L2: return False if L1: curr1 = L1.head if L2: curr2 = L2.head output = LinkedList() carry = 0 while curr1 or curr2: if not output.head: output.head = Node(0) curr3 = output.head else: curr3.next = Node(0) curr3 = curr3.next if curr1: curr3.value += curr1.value if curr2: curr3.value += curr2.value curr3.value += carry carry = 0 if curr3.value > 9: curr3.value = curr3.value % 10 carry = 1 if curr1: curr1 = curr1.next if curr2: curr2 = curr2.next return output
def add(self, val): # if val is a node, pass in if type(val) == type(Node()): self.add_helper(val) # otherwise cast to node else: self.add(Node(val))
def main(): # Build lists list_head = Node('genesis') ptr = list_head for b in range(50): ptr.next = Node(b) ptr = ptr.next # Copy and create circular list list_cir = copy.deepcopy(list_head) cir_ptr = list_cir while cir_ptr.next is not None: cir_ptr = cir_ptr.next cir_ptr.next = list_cir #p(list_cir) # 1 Infinity Loop.... Cupertino California # Check if linked list is circular. print('CIR? %s' % is_circular(list_cir)) print('TO REVERSE: ') p(list_head) print('IN REVERSE: ') p(Reverse(list_head)) print('Nth to the Last') print(NthToLast(15, list_head))
def init_linked_list(self): node_3 = Node(KeyValue(3, "val3")) self.linked_list.add_node(node_3) node_2 = Node(KeyValue(2, "val2")) self.linked_list.add_node(node_2) node_1 = Node(KeyValue(1, "val1")) self.linked_list.add_node(node_1)
def FindMergeNode(headA, headB): traverse1 = Node() traverse2 = Node() traverse1 = headA traverse2 = headB lenA = 0 lenB = 0 while traverse1 != None: lenA += 1 traverse1 = traverse1.next while traverse2 != None: lenB += 1 traverse2 = traverse2.next traverse1 = headA traverse2 = headB if lenA > lenB: extra = lenA - lenB while extra: extra -= 1 traverse1 = traverse.next else: if lenB > lenA: extra = lenB - lenA while extra: extra -= 1 traverse2 = traverse2.next while traverse1 != traverse2: traverse1 = traverse1.next traverse2 = traverse2.next return traverse1.data
def get_linked_list_from_stack(stack) -> LinkedList: ll = LinkedList() ll.set_head(Node(stack.pop())) while len(stack) > 0: ll.append_to_tail(Node(stack.pop())) return ll
def sum(ll1=LinkedList(), ll2=LinkedList()): result_list = LinkedList() summing_list, summed_list = swap_lists(ll1, ll2) carry = 0 c1, c2 = summing_list.head, summed_list.head while c1 and c2: sum = c1.value + c2.value + carry if sum >= 10: sum -= 10 carry = 1 else: carry = 0 result_list.append(Node(sum)) c1 = c1.next_node c2 = c2.next_node while c2: sum = c2.value + carry if sum >= 10: sum -= 10 carry = 1 else: carry = 0 result_list.append(Node(sum)) if not c2.next_node and carry == 1: result_list.append(Node(carry)) c2 = c2.next_node result_list.print_list()
def testLinkedList(): node1 = Node(None) print(node1.next) print(node1.data) node2 = Node(None) node1.setNext(node2) print(node2) print(node1.getNext()) if node2 == node1.getNext(): print("Linked List successfully created") else: print("An error in the node creation") mylist = UnorderedList() mylist.addElements(31) mylist.addElements(77) mylist.addElements(17) mylist.addElements(93) mylist.addElements(26) mylist.addElements(54) mylist.search(31) mylist.search(54) mylist.remove(54)
def test_insert_after_node(): linked_list = LinkedList() n1 = Node(1) n2 = Node(2) n3 = Node(3) n4 = Node(4) n5 = Node(5) # inserting into empty list linked_list.insert(None, n1) assert linked_list.head == n1 assert linked_list.tail == n1 linked_list.add_in_tail(n2) linked_list.add_in_tail(n3) # inserting into list middle linked_list.insert(n1, n4) assert n1.next == n4 assert n4.next == n2 # inserting into tail: linked_list.insert(n3, n5) assert n3.next == n5 assert n5.next is None assert linked_list.tail == n5
def sum_lists(l1, l2): borrow = 0 head = None tail = None while l1 or l2: sum_lists = borrow + (0 if not l1 else l1.val) sum_lists += 0 if not l2 else l2.val if sum_lists > 9: borrow = 1 sum_lists -= 10 else: borrow = 0 new_node = Node(sum_lists) if not head: head = new_node else: tail.next = new_node tail = new_node l1 = None if not l1 else l1.next l2 = None if not l2 else l2.next if borrow: tail.next = Node(borrow) tail = tail.next return head
def test_deleting_single_value(): linked_list = LinkedList() linked_list.delete(0) assert linked_list.head is None assert linked_list.tail is None # single-element list linked_list.add_in_tail(Node(1)) linked_list.delete(1) assert linked_list.len() == 0 assert linked_list.head is None assert linked_list.tail is None # multi-element list n1 = Node(1) n2 = Node(2) n3 = Node(3) n4 = Node(3) linked_list.add_in_tail(n1) linked_list.add_in_tail(n2) linked_list.add_in_tail(n3) linked_list.add_in_tail(n4) linked_list.delete(3) assert linked_list.head == n1 assert linked_list.tail == n4 assert linked_list.len() == 3
def main(): new_queue = custom_queue_stack() one = Node(1) two = Node(2) three = Node(3) new_queue.enqueue(one) new_queue.enqueue(two) print new_queue.peek().content
def naiveFillSmallLinkedList(val, nodeList, node): if node is None: node = Node(val) nodeList.head = node else: temp = Node(val) node.next = temp node = node.next return node, nodeList
def add(self, value): if (not self.first): self.first = Node(value) return True else: current = self.first while (current.next): current = current.next current.next = Node(value) return True
def test_2(self): n1 = Node(1) n2 = Node(2) n3 = Node(3) ll = LinkedList() ll.set_head(n1) ll.append_to_tail(n2, n3) assert loop_detection(ll) is None
def main(): one = Node(2) two = Node(3) three = Node(4) one.next = two list_one = CustomLinkedList() list_one.insert_at_the_beginning(one) list_one.insert_at_the_beginning(two) list_one.insert_at_the_beginning(three) four = find_start_of_loop(list_one)
def test_clean(): linked_list = LinkedList() linked_list.add_in_tail(Node(1)) linked_list.add_in_tail(Node(2)) linked_list.add_in_tail(Node(3)) linked_list.add_in_tail(Node(4)) linked_list.clean() assert linked_list.head is None assert linked_list.tail is None
def addFront(self, other): """ Adds the value to begin of the LinkedList. addFront: LinkedList other -> """ if self._head is None: self._head = Node(other) else: rest = self._head self._head = Node(other) self._head.next = rest
def test_2(self): ll = LinkedList() n1 = Node(1) n2 = Node(1) ll.set_head(n1) ll.append_to_tail(n2) remove_dups(ll) self.assertEqual(len(ll), 1)
def test_loop_detection_true(self): ll = LinkedList() head = Node(0) ll.head = head n = ll.head for i in range(1, 5): n.next = Node(i) n = n.next n.next = ll.head self.assertTrue(loop_detection(ll))
def test_loop_detection_runner_find_first_element(self): ll = LinkedList() head = Node(0) ll.head = head n = ll.head for i in range(1, 5): n.next = Node(i) n = n.next n.next = ll.head self.assertEqual(0, loop_detection_runner(ll).data)
def append(self, other): """ Adds the value to the end of the LinkedList. append: LinkedList other -> """ current = self._head if current: while current.next: current = current.next current.next = Node(other) else: self._head = Node(other)
def sum_lists(l1, l2): num1 = get_num(get_digits(l1)) num2 = get_num(get_digits(l2)) sum = num1 + num2 sum_digits = [int(x) for x in str(sum)] sum_digits.reverse() if len(sum_digits) == 0: return None sum_list = LinkedList(Node(sum_digits[0])) for i in range(1, len(sum_digits)): sum_list.add_node(Node(sum_digits[i])) return sum_list
def partition(node, partitionVal): head = node tail = node node = node.next while node: if node.value < partitionVal: newHead = Node(node.value, head) head = newHead else: tail.next = Node(node.value) tail = tail.next node = node.next return head
def addEdge(self, edge): ## Single edge object passed in, vertex determined from attributes of ## edge. List expanded if needed. Edge added as node to linked list vertex = edge.getStartVertex() if vertex + 1 > len(self.__AdjList): self.expandList(vertex) if self.__AdjList[vertex] == None: ## If no nodes have yet been added to vertex, edge set as head ## of linked list at vertex self.__AdjList[vertex] = NodeList(Node(edge)) else: self.__AdjList[vertex].add(Node(edge))
def test_longPalindrome(self): input = LinkedList() input.append(Node(5)) input.append(Node(3)) input.append(Node(5)) input.append(Node(3)) input.append(Node(0)) input.append(Node(9)) input.append(Node(3)) input.append(Node(5)) input.append(Node(3)) input.append(Node(5)) self.assertFalse(isPalindrome(input))
def main(): custom_linked_list = CustomLinkedList() node1 = Node(5) node2 = Node(7) node3 = Node(7) node4 = Node(8) node5 = Node(9) custom_linked_list.insert_at_the_beginning(node1) custom_linked_list.insert_at_the_beginning(node2) custom_linked_list.insert_at_the_beginning(node3) custom_linked_list.insert_at_the_beginning(node4) custom_linked_list.insert_at_the_beginning(Node(5)) custom_linked_list.print_list(custom_linked_list.head) print "\n" # linked_list = remove_duplicates_from_linkedL(custom_linked_list) # linked_list.print_list(linked_list.head) # linked_list_two = delete_without_using_HT(custom_linked_list) # linked_list_two.print_list(linked_list_two.head) # x = find_nth_to_the_last(custom_linked_list, 2) # print x.content # y = find_nth_with_my_list(custom_linked_list, 2) # print y.content one = Node(4) two = Node(3) list_one = CustomLinkedList() list_one.insert_at_the_beginning(one) list_one.insert_at_the_beginning(two) list_two = CustomLinkedList() list_two.insert_at_the_beginning(two) list_two.insert_at_the_beginning(one) three = add_numbers_using_linked_list(list_one.head, list_two.head, 0)
def test_flattening(): linked_list = LinkedList(head=Node(1)) linked_list.append(3) linked_list.append(5) second_linked_list = LinkedList(head=Node(2)) second_linked_list.append(4) nested_linked_list = NestedLinkedList(head=Node(linked_list)) nested_linked_list.append(second_linked_list) flattened = nested_linked_list.flatten() assert flattened.to_list() == [1, 2, 3, 4, 5], f"list contents: {flattened.to_list()}"
def setUp(self): ll = LinkedList() n1 = Node(1) n2 = Node(2) n3 = Node(3) n4 = Node(4) n5 = Node(5) ll.set_head(n1) ll.append_to_tail(n2) ll.append_to_tail(n3) ll.append_to_tail(n4) ll.append_to_tail(n5) self.ll = ll
def append(self, value): """Append new node with given value to the end of chain.""" last = self.first prev = self.first if self.first: while last.get_next() != self.first: prev = last last = last.get_next() if last: last.next_item = Node(value) last.next_item.next_item = self.first else: self.first = Node(value) self.first.next_item = self.first