else:
                        sv +=1
                        tempHead.next = slowNode
                        slowNode = slowNode.next
                    tempHead = tempHead.next

                while fv < i and fastNode != None:
                    tempHead.next = fastNode
                    tempHead = tempHead.next
                    fv +=1
                    fastNode = fastNode.next
                while sv < i and slowNode != None:
                    tempHead.next = slowNode
                    sv +=1
                    slowNode = slowNode.next
                    tempHead = tempHead.next

                # 这一句必须有,因为最后的时候给链表结尾用
                tempHead.next = fastNode
                slowNode = fastNode

            i*=2
        return  ans.next

if __name__ == '__main__':

    head = ListNode(-1)
    so = Solution()

    head.printOneList(so.sortList(head.getOneListNode([8,5,3,2,1])))


    def mergeTowList(self,first,second):
        if not first :
            return  second
        if not second:
            return  first
        head = curNode = ListNode(-1)
        while first != None and second != None:
            if first.val < second.val:
                curNode.next = first
                first = first.next
            else:
                curNode.next = second
                second = second.next
            curNode = curNode.next
        if first != None:
            curNode.next = first
        if second != None:
            curNode.next = second
        return  head.next

if __name__ == '__main__':
    node = ListNode(-1)
    so = Solution()
    first= node.getOneListNode([1,3,5,7,9,10,11])
    second = node.getOneListNode([2,4,6,8,1111])
    third = node.getOneListNode([10,11,12])
    lists = [first,second,third]
    node.printOneList(so.mergeKLists(lists))
Exemplo n.º 3
0
                        sv += 1
                        tempHead.next = slowNode
                        slowNode = slowNode.next
                    tempHead = tempHead.next

                while fv < i and fastNode != None:
                    tempHead.next = fastNode
                    tempHead = tempHead.next
                    fv += 1
                    fastNode = fastNode.next
                while sv < i and slowNode != None:
                    tempHead.next = slowNode
                    sv += 1
                    slowNode = slowNode.next
                    tempHead = tempHead.next

                # 这一句必须有,因为最后的时候给链表结尾用
                tempHead.next = fastNode
                slowNode = fastNode

            i *= 2
        return ans.next


if __name__ == '__main__':

    head = ListNode(-1)
    so = Solution()

    head.printOneList(so.sortList(head.getOneListNode([8, 5, 3, 2, 1])))