Ejemplo n.º 1
0
    def addTwoNumbers(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        if not l1:
            return l2
        if not l2:
            return l1

        dummy_node = SingleLinkedNode()
        node = dummy_node
        carry = 0
        while l1 is not None or l2 is not None or carry != 0:
            if l1 is not None:
                carry += l1.value
                l1 = l1.next
            if l2 is not None:
                carry += l2.value
                l2 = l2.next

            temp_node = SingleLinkedNode()
            temp_node.value = carry % 10
            node.next = temp_node

            node = node.next
            carry = carry // 10

        return dummy_node.next
Ejemplo n.º 2
0
    def addTwoNumbers2(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        if not l1:
            return l2
        if not l2:
            return l1

        stack_l1 = deque()
        while l1 is not None:
            stack_l1.append(l1.value)
            l1 = l1.next
        stack_l2 = deque()
        while l2 is not None:
            stack_l2.append(l2.value)
            l2 = l2.next
        dummy_node = None
        carry = 0
        while len(stack_l1) != 0 or len(stack_l2) != 0 or carry != 0:
            if len(stack_l1) != 0:
                carry += stack_l1.pop()
            if len(stack_l2) != 0:
                carry += stack_l2.pop()
            new_node = SingleLinkedNode()
            new_node.value = carry % 10
            new_node.next = dummy_node
            dummy_node = new_node
            carry //= 10
        return dummy_node
Ejemplo n.º 3
0
    def mergeTwoLists(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        if l1 is None:
            return l2
        if l2 is None:
            return l1

        dummy_node = SingleLinkedNode()
        node = dummy_node

        while l1 is not None and l2 is not None:
            if l1.value < l2.value:
                node.next = l1
                l1 = l1.next
            else:
                node.next = l2
                l2 = l2.next
            node = node.next

        if l1 is None:
            node.next = l2
        else:
            node.next = l1

        return dummy_node.next
Ejemplo n.º 4
0
 def swapPairs_iteration(self, head):
     dummy = pre = SingleLinkedNode()
     pre.next = head
     while pre.next and pre.next.next:
         a = pre.next
         b = a.next
         pre.next, a.next, b.next = b, b.next, a
         pre = a
     return dummy.next
 def test_swapPairs_one_node_iteration(self):
     input_node = SingleLinkedNode()
     input_node.value = 1
     head = self.solution.swapPairs(input_node)
     self.assertEqual(input_node, head)
 def test_removeNthFromEnd_1element_k_equals_1(self):
     input_node = SingleLinkedNode()
     input_node.value = 1
     head = self.solution.removeNthFromEnd(input_node, 1)
     self.assertIsNone(head)
 def test_reverseList_iterate_single_node(self):
     input_node = SingleLinkedNode()
     node = self.solution.reverseList_iteration(input_node)
     self.assertEqual(input_node, node)