def sortList(self, head): """ :type head: ListNode :rtype: ListNode """ if not head: return head if head.next == None: return head listLen = 0 tempHead = head while tempHead != None: tempHead = tempHead.next listLen+=1 i = 1 ans = ListNode(-1) ans.next = head while i < listLen: tempHead = ans fastNode = tempHead.next slowNode = tempHead.next while fastNode != None or slowNode != None: fv = 0 sv = 0 # 较快的指针先走i个节点 while fv < i and fastNode != None: fastNode = fastNode.next fv +=1 fv = 0 # 和并慢的指针和快的指针 while fv < i and sv < i and fastNode != None and slowNode != None: if fastNode.val < slowNode.val: fv +=1 tempHead.next = fastNode fastNode = fastNode.next 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
def sortList(self, head): """ :type head: ListNode :rtype: ListNode """ if not head: return head if head.next == None: return head listLen = 0 tempHead = head while tempHead != None: tempHead = tempHead.next listLen += 1 i = 1 ans = ListNode(-1) ans.next = head while i < listLen: tempHead = ans fastNode = tempHead.next slowNode = tempHead.next while fastNode != None or slowNode != None: fv = 0 sv = 0 # 较快的指针先走i个节点 while fv < i and fastNode != None: fastNode = fastNode.next fv += 1 fv = 0 # 和并慢的指针和快的指针 while fv < i and sv < i and fastNode != None and slowNode != None: if fastNode.val < slowNode.val: fv += 1 tempHead.next = fastNode fastNode = fastNode.next 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
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
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))
fastNode = fastNode.next 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])))
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])))