예제 #1
0
    def firstImpl(self, l1, l2):

        if l1 is None:
            return l2
        if l2 is None:
            return l1

        ptr1 = l1
        ptr2 = l2

        if ptr1.val < ptr2.val:
            head = ListNode(ptr1.val)
            ptr1 = ptr1.next
        else:
            head = ListNode(ptr2.val)
            ptr2 = ptr2.next

        track = head
        while ptr1 is not None or ptr2 is not None:

            val1 = ptr1.val if ptr1 else float("inf")
            val2 = ptr2.val if ptr2 else float("inf")

            if val1 < val2:
                track.next = ListNode(val1)
                ptr1 = ptr1.next if ptr1 else None
            else:
                track.next = ListNode(val2)
                ptr2 = ptr2.next if ptr2 else None

            track = track.next

        return head
예제 #2
0
    def testCase(self):
        head1 = ListNode(1)
        # head1.next = ListNode(4)
        # head1.next.next = ListNode(3)

        head2 = ListNode(9)
        head2.next = ListNode(9)
        # head2.next.next = ListNode(4)

        return head1, head2
예제 #3
0
    def constructCase(self):
        from customDataType import ListNode
        head1 = ListNode(1)
        head1.next = ListNode(2)
        head1.next.next = ListNode(4)

        head2 = ListNode(1)
        head2.next = ListNode(3)
        head2.next.next = ListNode(4)
        return head1, head2
예제 #4
0
    def constructCase(self, lst, target_val):
        from customDataType import ListNode

        node = ListNode(lst.pop(0))
        target_node = None
        if node.val == target_val:
            target_node = node

        head = node
        while lst:
            node.next = ListNode(lst.pop(0))
            if node.val == target_val:
                target_node = node
            node = node.next

        return head, target_node
예제 #5
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

        head = ListNode(float("-inf"))

        track = head

        while l1 and l2:

            if l1.val < l2.val:
                track.next = l1
                l1 = l1.next
            else:
                track.next = l2
                l2 = l2.next

            track = track.next

        track.next = l1 or l2

        return head.next
 def testCase(self):
     from customDataType import ListNode
     head = ListNode(1)
     head.next = ListNode(2)
     head.next.next = ListNode(3)
     head.next.next.next = ListNode(4)
     head.next.next.next.next = ListNode(5)
     return head
예제 #7
0
    def sortedListToBST(self, head):
        """
        :type head: ListNode
        :rtype: TreeNode
        """

        node = head
        if node is None:
            return None
        if node.next is None:
            return TreeNode(node.val)

        # transfer linked list to array
        array_whole = [node.val]
        while node.next:
            node = node.next
            array_whole.append(node.val)

        mid = int(len(array_whole) / 2)
        root = TreeNode(array_whole[mid])

        # split the array
        array_l = array_whole[:mid]
        array_r = [] if mid == len(array_whole) - 1 else array_whole[mid + 1:]

        # processing left
        if len(array_l) > 0:
            node = ListNode(array_l.pop(0))
            head_l = node
            while array_l:
                node.next = ListNode(array_l.pop(0))
                node = node.next
            root.left = self.sortedListToBST(head_l)
        else:
            root.left = None

        # processing right
        if len(array_r) > 0:
            node = ListNode(array_r.pop(0))
            head_r = node
            while array_r:
                node.next = ListNode(array_r.pop(0))
                node = node.next
            root.right = self.sortedListToBST(head_r)
        else:
            root.right = None

        return root
예제 #8
0
    def swapPairs(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """

        p = head
        dummy = ListNode(0)
        dummy.next = head
        prev = dummy
        while p and p.next:
            # initialize
            q = p.next
            r = q.next
            # swap
            prev.next = q
            q.next = p
            p.next = r
            # take steps forward
            prev = p
            p = r

        return dummy.next
예제 #9
0
    def addTwoNumbers(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        if l1 is None:
            return l2
        if l2 is None:
            return l1

        track = ListNode(0)
        head = track
        overflow = 0
        while l1 or l2:
            s = 0
            if l1:
                s += l1.val
                l1 = l1.next
            if l2:
                s += l2.val
                l2 = l2.next

            s += overflow
            track.val = s % 10
            overflow = int(s / 10)
            # print("s: {}, val: {}, ov: {}".format(s, track.val, overflow))
            if l1 or l2:
                track.next = ListNode(0)
                track = track.next

        if l1 is None and l2 is None:
            if overflow > 0:
                track.next = ListNode(overflow)

        return head
예제 #10
0
 def printList(self, head):
     from customDataType import ListNode
     ListNode.printList(head)
예제 #11
0
 def testCase(self):
     head = ListNode(1)
     head.next = ListNode(2)
     head.next.next = ListNode(3)
     head.next.next.next = ListNode(4)
     return head