Esempio n. 1
0
    def test_reverse(self):
        """Test for reverse

        Args:
            self: TestLinkedList

        Returns:
            None

        Raises:
            None
        """
        # Given
        input_linked_list = LinkedList()
        input_linked_list.append(Node(1))
        input_linked_list.append(Node(2))
        input_linked_list.append(Node(3))
        input_linked_list.append(Node(4))
        input_linked_list.append(Node(5))

        # When
        input_linked_list.reverse()

        # Then
        self.assertEqual(input_linked_list.output_list(), [5, 4, 3, 2, 1])
    def solve(self):
        """Solve the problem

        Note: O(n) (runtime) solution works by simultaneously iterating over both the list and taking the carry to the digit.

        Args:

        Returns:
            list

        Raises:
            None
        """
        print("Solving {} problem ...".format(self.PROBLEM_NAME))

        node1 = self.input_linked_list1.head
        node2 = self.input_linked_list2.head

        carry = 0
        sum_list = LinkedList()
        while node1 is not None and node2 is not None:
            digit1 = node1.data
            digit2 = node2.data

            digit_sum = digit1 + digit2 + carry

            if digit_sum > 9:
                carry = 1
            else:
                carry = 0

            sum_list.append(Node(digit_sum % 10))

            node1 = node1.next_node
            node2 = node2.next_node

        node = None
        if node1 is None and node2 is not None:
            node = node2
        elif node1 is not None and node2 is None:
            node = node1

        while node is not None:
            digit_sum = node.data + carry
            if digit_sum > 9:
                carry = 1
            sum_list.append(Node(digit_sum % 10))
            node = node.next_node

        return list(reversed(sum_list.output_list()))
Esempio n. 3
0
    def solve(self):
        """Solve the problem

        Note: O(n) (runtime) and O(1) (space) solution works by iterating over both the linked lists
              and appending the smallest one to the merged_list.

        Args:

        Returns:
            list

        Raises:
            None
        """
        print("Solving {} problem ...".format(self.PROBLEM_NAME))

        merged_list = LinkedList()

        node1 = self.input_linked_list1.head
        node2 = self.input_linked_list2.head

        # iterate over both the lists and append the smallest
        while node1 is not None and node2 is not None:
            if node1.data < node2.data:
                merged_list.append(Node(node1.data))
                node1 = node1.next_node
            else:
                merged_list.append(Node(node2.data))
                node2 = node2.next_node

        # append the remaining elements from the second list
        if node1 is None:
            while node2 is not None:
                merged_list.append(Node(node2.data))
                node2 = node2.next_node

        # append the remaining elements from the first list
        if node2 is None:
            while node1 is not None:
                merged_list.append(Node(node1.data))
                node1 = node1.next_node

        return merged_list.output_list()
Esempio n. 4
0
    def test_construct(self):
        """Test for construct

        Args:
            self: TestLinkedList

        Returns:
            None

        Raises:
            None
        """
        # Given
        input_linked_list = LinkedList()
        input_linked_list.append(Node(1))
        input_linked_list.append(Node(2))
        input_linked_list.append(Node(3))
        input_linked_list.append(Node(4))
        input_linked_list.append(Node(5))

        # Then
        self.assertEqual(input_linked_list.output_list(), [1, 2, 3, 4, 5])