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. 链表排序可以用很多方法,插入,冒泡,选择都可以,也容易实现 ...
随机推荐
- 选择webpack这条路,我踩过的坑
在http://www.jianshu.com/p/42e11515c10f这篇文章,我重新学习了一下webpack. 一.json文件里面不应该含有注释,否则会报错 本来我的json文件长这个样子: ...
- struts-2.5.14.1 中web.xml的基本配置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http:// ...
- Apache httpclient的execute方法调试
因为工作需要,想研究一下execute执行的逻辑. 在这一行调用execute: response = getHttpClient().execute(get); getHttpClient的实现: ...
- textbox 输入值提示。。。(类似百度搜索)
public void textBox5_xiala() { DataSet ds = SQl_yuji.ds_cinvname(); this.textBox5.AutoCompleteMode = ...
- 多线程threading模块
python的多线程编程 简介 多线程编程技术可以实现代码并行性,优化处理能力,同时功能的更小划分可以使代码的可重用性更好.Python中threading和Queue模块可以用来实现多线程编程. 详 ...
- 2.03_Python网络爬虫Http和Https
一:HTTP和HTTPS HTTP协议(HyperText Transfer Protocol,超文本传输协议):是一种发布和接收 HTML页面的方法,以明文的形式传输,效率高,但是不安全 HTTPS ...
- axios 用 params/data 发送参数给 springboot controller,如何才能正确获取
今天有人遇到接口调用不通的情况,粗略看了一下是axios跨域请求引起了.找到问题,处理就简单多了. 但是我看其代码,发现比较有意思 export function agentlist(query) { ...
- bat 判断命令是否执行成功
bat 判断命令是否执行成功 连接符形式,&& 表示成功,|| 表示失败,例如: call xxx.bat && (goto succeed) || goto fail ...
- 【Amaple教程】4. 组件
在Amaple单页应用中,一个页面其实存在两种模块化单位,分别是 模块 (am.Module类),它是以web单页应用跳转更新为最小单位所拆分的独立块: 组件 (am.Component类),它的定位 ...
- Java实现复制文件或者文件夹
拷贝一个文件的算法比较简单,当然,可以对它进行优化,比如使用缓冲流,提高读写数据的效率等. 话不多说直接上代码 import java.io.*; /** * 实现文件的拷贝 */ public cl ...