[leetcode]Sort List @ Python
原题地址:http://oj.leetcode.com/problems/sort-list/
题意:链表的排序。要求:时间复杂度O(nlogn),空间复杂度O(1)。
解题思路:由于题目对时间复杂度和空间复杂度要求比较高,所以查看了各种解法,最好的解法就是归并排序,由于链表在归并操作时并不需要像数组的归并操作那样分配一个临时数组空间,所以这样就是常数空间复杂度了,当然这里不考虑递归所产生的系统调用的栈。
这里涉及到一个链表常用的操作,即快慢指针的技巧。设置slow和fast指针,开始它们都指向表头,fast每次走两步,slow每次走一步,fast到链表尾部时,slow正好到中间,这样就将链表截为两段。
运行时需要将中文注释删掉,leetcode oj平台里面不支持中文字符。
代码:
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None class Solution:
# @param head, a ListNode
# @return a ListNode
def merge(self, head1, head2):
if head1 == None: return head2
if head2 == None: return head1
dummy = ListNode(0) #归并时,新建一个链表头结点
p = dummy
while head1 and head2:
if head1.val <= head2.val:
p.next = head1
head1 = head1.next
p = p.next
else:
p.next = head2
head2 = head2.next
p = p.next
if head1 == None:
p.next = head2
if head2 == None:
p.next = head1
return dummy.next def sortList(self, head):
if head == None or head.next == None:
return head
slow = head; fast = head #快慢指针技巧的运用,用来截断链表。
while fast.next and fast.next.next:
slow = slow.next
fast = fast.next.next
head1 = head
head2 = slow.next
slow.next = None #head1和head2为截为两条链表的表头
head1 = self.sortList(head1)
head2 = self.sortList(head2)
head = self.merge(head1, head2)
return head
[leetcode]Sort List @ Python的更多相关文章
- [leetcode]Sort Colors @ Python
原题地址:https://oj.leetcode.com/problems/sort-colors/ 题意: Given an array with n objects colored red, wh ...
- LeetCode—-Sort List
LeetCode--Sort List Question Sort a linked list in O(n log n) time using constant space complexity. ...
- [LeetCode]题解(python):147-Insertion Sort List
题目来源: https://leetcode.com/problems/insertion-sort-list/ 题意分析: 用插入排序排序一个链表. 题目思路: 这题没什么好说的,直接用插入排序就行 ...
- [leetcode]Insertion Sort List @ Python
原题地址:http://oj.leetcode.com/problems/insertion-sort-list/ 题意:对链表进行插入排序. 解题思路:首先来对插入排序有一个直观的认识,来自维基百科 ...
- [LeetCode]题解(python):090 Subsets II
题目来源 https://leetcode.com/problems/subsets-ii/ Given a collection of integers that might contain dup ...
- [LeetCode]题解(python):078 Subsets
题目来源 https://leetcode.com/problems/subsets/ Given a set of distinct integers, nums, return all possi ...
- [LeetCode]题解(python):049-Groups Anagrams
题目来源 https://leetcode.com/problems/anagrams/ Given an array of strings, group anagrams together. For ...
- [LeetCode]题解(python):047-Permutations II
题目来源 https://leetcode.com/problems/permutations-ii/ Given a collection of numbers that might contain ...
- [LeetCode]题解(python):057-Insert Interval
题目来源 https://leetcode.com/problems/insert-interval/ Given a set of non-overlapping intervals, insert ...
随机推荐
- java jdbc深入理解(connection与threadlocal与数据库连接池和事务实)
1.jdbc连接数据库,就这样子 Class.forName("com.mysql.jdbc.Driver");java.sql.Connection conn = DriverM ...
- (一)预定义宏、__func__、_Pragma、变长参数宏定义以及__VA_ARGS__
作为第一篇,首先要说一下C++11与C99的兼容性. C++11将 对以下这些C99特性的支持 都纳入新标准中: 1) C99中的预定义宏 2) __func__预定义标识符 3) _Pragma操作 ...
- [Python网络编程]一个简单的TCP时间服务器
服务器端: 1.创建一个面向网络的TCP套接字对象socket, 2.绑定地址和端口 3.监听 4.当有客户端连接时候,接受连接并给此连接分配一个新的套接字 5.当客户端发送空信息时候,关闭新分配的套 ...
- hdu 3613 扩展kmp+回文串
题目大意:给个字符串S,要把S分成两段T1,T2,每个字母都有一个对应的价值,如果T1,T2是回文串(从左往右或者从右往左读,都一样),那么他们就会有一个价值,这个价值是这个串的所有字母价值之和,如果 ...
- linux下elasticsearch安装教程
centos 7.5安装 elasticsearch 第一步,安装elasticsearch需要Java8 首先使用 yum list installed | grep java 查看安装的Java版 ...
- bzoj 4092 DP
简化题意: 给定两个集合A,B,A集合有一个权值,并且对应一个B集合的子集,求A的一个子集,满足权值和最小且对应的子集的并集是B集合. 感觉像网络流,但因为每个B中的元素对应一个A中的元素就行了,是o ...
- 【转载】VC操作剪切板
1.在剪切板上放置数据 if(OpenClipboard()) //打开剪切板{ EmptyClipboard(); //清空剪切板 CString str; //从控件 ...
- codevs 1204 寻找子串位置 KMP
1204:寻找子串位置 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 18K Solved: 8K Description 给出字符串a和字符串b,保 ...
- interfacer和abstarct class的异同
- MYSQL分段统计
产品表 CREATE TABLE `product` ( `product_id` int(11) NOT NULL AUTO_INCREMENT, `product_model` varchar(2 ...