mycode    97.37%

如果用上一个题目中”参考“的方法,res中放节点而不是val,就会超时

# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None class Solution(object):
def sortList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
res = []
while head:
res.append(head.val)
head = head.next
res.sort() dummy = head = ListNode(-1)
for l in res:
head.next = ListNode(l)
head = head.next
return dummy.next

参考

二分法!!!!!!!

# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None class Solution(object):
def sortList(self, head):
if not head or not head.next:
return head
slow, fast = head, head.next
while fast and fast.next:
slow = slow.next
fast = fast.next.next
second = slow.next
slow.next = None
first_half = self.sortList(head)
second_half = self.sortList(second)
return self.merge(first_half, second_half) def merge(self,l1, l2):
if not l1 or not l2:
return l2 or l1
s, e = None, None
while l1 and l2:
if l1.val > l2.val:
if not s:
s = e = l2
l2 = l2.next
else:
e.next = l2
e = e.next
l2 = l2.next
else:
if not s:
s = e = l1
l1 = l1.next
else:
e.next = l1
e = e.next
l1 = l1.next
e.next = l1 or l2
return s

leetcode-hard-ListNode-148. Sort List的更多相关文章

  1. C#版 - LeetCode 148. Sort List 解题报告(归并排序小结)

    leetcode 148. Sort List 提交网址: https://leetcode.com/problems/sort-list/  Total Accepted: 68702 Total ...

  2. Leetcode之148. Sort List Medium

    https://leetcode.com/problems/sort-list/ Sort a linked list in O(n log n) time using constant space ...

  3. 148. Sort List - LeetCode

    Solution 148. Sort List Question 题目大意:对链表进行排序 思路:链表转为数组,数组用二分法排序 Java实现: public ListNode sortList(Li ...

  4. 【LeetCode】147. Insertion Sort List 解题报告(Python)

    [LeetCode]147. Insertion Sort List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...

  5. [LeetCode] 148. Sort List 链表排序

    Sort a linked list in O(n log n) time using constant space complexity. Example 1: Input: 4->2-> ...

  6. [LeetCode] 148. Sort List 解题思路

    Sort a linked list in O(n log n) time using constant space complexity. 问题:对一个单列表排序,要求时间复杂度为 O(n*logn ...

  7. 【LeetCode】148. Sort List 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  8. Java for LeetCode 148 Sort List

    Sort a linked list in O(n log n) time using constant space complexity. 解题思路: 归并排序.快速排序.堆排序都是O(n log ...

  9. leetcode 148. Sort List ----- java

    Sort a linked list in O(n log n) time using constant space complexity. 排序,要求是O(nlog(n))的时间复杂度和常数的空间复 ...

  10. 【leetcode】148. Sort List

    Sort a linked list in O(n log n) time using constant space complexity. 链表排序可以用很多方法,插入,冒泡,选择都可以,也容易实现 ...

随机推荐

  1. css改变鼠标指针的形状

    <html> <body> <span style="cursor:auto"> <br /> <span style=&qu ...

  2. Spring Cloud(四)服务提供者 Eureka + 服务消费者 Feign

    上一篇文章,讲述了如何通过RestTemplate + Ribbon去消费服务,这篇文章主要讲述如何通过Feign去消费服务. Feign简介 Feign是一个声明式的伪Http客户端,它使得写Htt ...

  3. BootStrap【二、样式】

    H5文档类型 由于使用了H5和CSS熟悉,需要在文件头引入 移动设备优先 为了对移动设备友好,需要使用标签viewport width=device-width 宽度为设备宽度 height 高度 i ...

  4. RobHess的SIFT代码解析步骤四

    平台:win10 x64 +VS 2015专业版 +opencv-2.4.11 + gtk_-bundle_2.24.10_win32 主要参考:1.代码:RobHess的SIFT源码 2.书:王永明 ...

  5. RobHess的SIFT代码解析步骤三

    平台:win10 x64 +VS 2015专业版 +opencv-2.4.11 + gtk_-bundle_2.24.10_win32 主要参考:1.代码:RobHess的SIFT源码 2.书:王永明 ...

  6. java_实现Hello World

    1.新建项目 在空白处右击--New--java Project 2.项目文件结构 新建了项目之后项目文件在工作空间里面,(如果忘记工作空间的路径可以点击File---Switch Workspace ...

  7. List去重比较

    import java.util.ArrayList; import java.util.HashSet; import java.util.LinkedHashSet; import java.ut ...

  8. Java实现一行一行读取文件内容(进行编码处理)

    // 读取文件内容public String readFile(){ String path = ""; File file = new File(path); StringBui ...

  9. Series拼接回DataFrame

    从这样的表,如何计算一行汇总层拼接回去

  10. BZOJ 1188 / Luogu P3185 [HNOI2007]分裂游戏 (SG函数)

    题意 有n个格子,标号为0 ~ n-1,每个格子上有若干石子,每次操作可以选一个0 ~ n-2的格子上的一颗石子,分裂为两颗,然后任意放在后面的两个格子内,这两个格子可以相同.求使先手必胜的第一步的方 ...