leetcode-hard-ListNode-148. Sort List
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的更多相关文章
- C#版 - LeetCode 148. Sort List 解题报告(归并排序小结)
leetcode 148. Sort List 提交网址: https://leetcode.com/problems/sort-list/ Total Accepted: 68702 Total ...
- Leetcode之148. Sort List Medium
https://leetcode.com/problems/sort-list/ Sort a linked list in O(n log n) time using constant space ...
- 148. Sort List - LeetCode
Solution 148. Sort List Question 题目大意:对链表进行排序 思路:链表转为数组,数组用二分法排序 Java实现: public ListNode sortList(Li ...
- 【LeetCode】147. Insertion Sort List 解题报告(Python)
[LeetCode]147. Insertion Sort List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
- [LeetCode] 148. Sort List 链表排序
Sort a linked list in O(n log n) time using constant space complexity. Example 1: Input: 4->2-> ...
- [LeetCode] 148. Sort List 解题思路
Sort a linked list in O(n log n) time using constant space complexity. 问题:对一个单列表排序,要求时间复杂度为 O(n*logn ...
- 【LeetCode】148. Sort List 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- Java for LeetCode 148 Sort List
Sort a linked list in O(n log n) time using constant space complexity. 解题思路: 归并排序.快速排序.堆排序都是O(n log ...
- leetcode 148. Sort List ----- java
Sort a linked list in O(n log n) time using constant space complexity. 排序,要求是O(nlog(n))的时间复杂度和常数的空间复 ...
- 【leetcode】148. Sort List
Sort a linked list in O(n log n) time using constant space complexity. 链表排序可以用很多方法,插入,冒泡,选择都可以,也容易实现 ...
随机推荐
- K2 BPM_K2受邀出席SAP研讨会:共话“智能+”时代下的赋能与转型_全业务流程管理专家
3月5日,第十三届全国人大二次会议在北京召开.政府工作报告首次出现“智能+”,并明确指出2019年,要打造工业互联网平台,拓展“智能+”,为制造业转型升级赋能.从政府工作报告中不难看出,“智能+” ...
- Oracle权限管理详解(2)
详见:https://blog.csdn.net/u013412772/article/details/52733050 Oracle数据库推荐以引用博客: http: http:.html http ...
- 修正Calendar的Bug
procedure TAndroidNativeCalendarListener.onSelectedDayChange(view: JCalendarView; year, month, dayOf ...
- 【jekins】jenkins构建触发
一.定时构建的语法 * * * * *(五颗星,中间用空格隔开)第一颗*表示分钟,取值0~59第二颗*表示小时,取值0~23第三颗*表示一个月的第几天,取值1~31第四颗*表示第几月,取值1~12第五 ...
- TCP 的三次握手和四次挥手,TCP 的流量控制和拥塞控制
70.TCP协议的三次握手与四次挥手70.1.TCP报文结构 1.源端口号:表示发送端端口号,字段长为16位. 2.目标端口号:表示接收端口号,字段长为16位. 3.序列号:表示发送数据的位置 ...
- 关于Linux连接工具mobaxterm显示中文乱码问题
本人用的是MobaXterm Personal 9.1版本.近期发现连接上服务器,查看日志时,发现中文乱码,无法正常显示.甚是苦恼.百度搜索该工具显示乱码问题,无一人解决.提倡更换连接工具.无意间发现 ...
- python 套接字Socket详解
socket简介 1. 什么是socket ? socket(简称 套接字) 是进程间通信的一种方式,它与其他进程间通信的一个主要不同是: 它能实现不同主机间的进程间通信,我们网络上各种各样的服务大多 ...
- redis,windows设置记录
windows下载 github地址 : https://github.com/MicrosoftArchive/redis/releases #设置内存 redis-server.exe redis ...
- 使用html2canvas在手机端独立实现h5页面转图片
需求 方便用户把每日消息的海报图片分享到微信朋友圈进行消息扩散 实现方案 使用html2canvas 插件,html2canvas 1.0.0-alpha.11 ,github地址:https://g ...
- 干物妹小埋 (离散化 + 线段树 + DP)
链接:https://ac.nowcoder.com/acm/contest/992/B来源:牛客网 题目描述 在之前很火的一个动漫<干物妹小埋>中,大家对小埋打游戏喝可乐的印象十分的深刻 ...