def test_no_loop(self): self.assertFalse(loop_detection_buffer(self.ll)) self.ll.next = Node(1) self.ll.next.next = Node(3) self.assertFalse(loop_detection_buffer(self.ll))
def sum_linked_lists(node1, node2, carry=0): """Recursive solution summing two linked lists without explicit Singly Linked List Class :param node1 node of first linked list :param node2 node of second linked list :param carry should be defaulted to zero :returns Sum of linked lists """ if node1 is None and node2 is None and carry == 0: return data = carry if node1 is not None: data += node1.data node1 = node1.next if node2 is not None: data += node2.data node2 = node2.next curr = Node(data % 10) curr.next = sum_linked_lists(node1, node2, int(data >= 10)) return curr
def test_more_two_nodes(self): """Delete middle node in linked list of length > two elements""" node = Node(1) node.next = Node(3) node.next.next = Node(4) delete_middle_node(node.next) self.assertTrue(node.next.data == 4)
def test_two_nodes(self): """Deletes head node in linked list with two elements""" node = Node(1) node.next = Node(2) delete_middle_node(node) self.assertIsNone(node.next) self.assertTrue(node.data == 2)
def test_multiple_elements(self): self.ll.next = Node(1) # No loop self.assertFalse(loop_detection(self.ll)) # Loop at end self.ll.next.next = self.ll.next self.assertTrue(loop_detection(self.ll) == self.ll.next) # Loop at beginning self.ll.next.next = Node(2) self.ll.next.next.next = self.ll.next self.assertTrue(loop_detection(self.ll) == self.ll.next)
def pad_zeroes(head, num): """Pad zeroes to the front of the linked list :param head head of the linked list :param num number of zeroes to add :returns linked list with zeroes padded to the front """ new = curr = Node(0) num -= 1 while num > 0: curr.next = Node(0) curr = curr.next num -= 1 curr.next = head return new
def test_even_nodes(self): l_list = Node(1) l_list.next = Node(2) l_list.next = Node(2) l_list.next = Node(3) self.assertFalse(is_palindrome_reverse(l_list)) l_list.next.next = Node(1) self.assertTrue(is_palindrome_reverse(l_list))
def test_linked_list_greater_one_element(self): ll_a = Node(1) ll_a.next = Node(3) ll_a.next.next = Node(4) ll_b = Node(0) ll_b.next = ll_a.next.next # Same Node self.assertTrue(is_intersection(ll_a, ll_a) == ll_a) # Last Node, different sizes self.assertTrue(is_intersection(ll_a, ll_b) == ll_b.next) # Last Node, same sizes ll_b.next = Node(4) ll_b.next.next = ll_a.next.next self.assertTrue(is_intersection(ll_a, ll_b) == ll_b.next.next)
def test_loop(self): self.ll.next = self.ll # Single element self.assertTrue(loop_detection_buffer(self.ll) == self.ll) # Loop at beginning self.ll.next = Node(1) self.ll.next.next = self.ll self.assertTrue(loop_detection_buffer(self.ll) == self.ll) # Loop in middle self.ll.next.next = Node(2) self.ll.next.next = self.ll.next self.assertTrue(loop_detection_buffer(self.ll) == self.ll.next) # Loop at end self.ll.next.next.next = self.ll.next.next self.assertTrue(loop_detection_buffer(self.ll) == self.ll.next.next)
def test_even_elements(self): list = Node(1) list.next = Node(2) list.next.next = Node(2) list.next.next.next = Node(3) self.assertFalse(is_palindrome_brute_force(list)) list.next.next.next = Node(1) self.assertTrue(is_palindrome_brute_force(list))
def sum_helper(node1, node2): """Sum helper function to recursively sum elements of equal length linked lists :param node1 head of first linked list :param node2 head of second linked list :returns sum of first and second linked lists as a set of nodes """ if node1 is None and node2 is None: return curr = sum_helper(node1.next, node2.next) if curr is None: curr = Node(0) data = node1.data + node2.data + curr.data curr.data = data % 10 next = Node(int(data >= 10)) next.next = curr return next
def test_single_empty_linked_list(self): ll_a = Node(1) self.assertFalse(is_intersection(ll_a, None)) self.assertFalse(is_intersection(None, ll_a))
def test_single_node(self): """Delete node with one element in linked list, raise IndexError""" node = Node(1) with self.assertRaises(IndexError): delete_middle_node(node)
def setUp(self): self.ll = Node(0)
def test_linked_list_intersection_single_element(self): ll_a = Node(1) ll_b = Node(2) self.assertTrue(is_intersection(ll_a, ll_a) is ll_a) self.assertFalse(is_intersection(ll_a, ll_b))
def test_single_element(self): list = Node(1) self.assertTrue(is_palindrome_brute_force(list))
def test_single_node(self): self.assertTrue(is_palindrome_reverse(Node(1)))