예제 #1
0
    def test_two_elements(self):
        list = Node(1)
        list.next = Node(2)

        self.assertFalse(is_palindrome_brute_force(list))

        list.next = Node(1)
        self.assertTrue(is_palindrome_brute_force(list))
예제 #2
0
    def test_two_nodes(self):
        l_list = Node(1)
        l_list.next = Node(2)

        self.assertFalse(is_palindrome_reverse(l_list))

        l_list.next = Node(1)
        self.assertTrue(is_palindrome_reverse(l_list))
예제 #3
0
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
예제 #4
0
    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)
예제 #5
0
    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)
예제 #6
0
    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)
예제 #7
0
    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