【Leetcode】Sort List (Sorting)
这个问题需要与归并排序排两个名单,基本思路分为切割与合并
合并后的代码Merge Two Sorted List里已经讲得非常清楚了。
所以这里直接给出代码。
public ListNode merge(ListNode l1, ListNode l2) {
ListNode helper = new ListNode(0);
ListNode runner = helper;
while (l1 != null && l2 != null) {
if (l1.val < l2.val) {
runner.next = l1;
l1 = l1.next;
runner = runner.next;
} else {
runner.next = l2;
l2 = l2.next;
runner = runner.next;
}
if (l1 != null)
runner.next = l1;
if (l2 != null)
runner.next = l2;
}
return helper.next;
}
切割的思想非常重要,在数组里面是通过切割知道仅仅剩下一个元素的时候结束,链表也一样。
数组中推断仅仅有一个元素的方法是if(left==right)
链表中推断仅仅有一个元素的方法是if(head.next==null)
数组中的切割的方法是
int middle = (left + right) / 2;
mergeSort(array, left, middle);
mergeSort(array, middle + 1, right);
merge(array, left, middle, right);
链表中怎么找middle呢?这个时候我们想起了前面所提到的Walker_Runner技术来找中点。
ListNode walker = head;
ListNode runner = head;
while (runner.next != null && runner.next.next != null) {
walker = walker.next;
runner = runner.next.next;
}
ListNode head2 = walker.next;
walker.next = null;
head = mergeSort(head);
head2 = mergeSort(head2);
return merge(head, head2);
以下给出完整的代码
public ListNode sortList(ListNode head) {
if (head == null)
return head;
return mergeSort(head);
}
public ListNode mergeSort(ListNode head) {
if (head.next == null)
return head;
ListNode walker = head;
ListNode runner = head;
while (runner.next != null && runner.next.next != null) {
walker = walker.next;
runner = runner.next.next;
}
ListNode head2 = walker.next;
walker.next = null;
head = mergeSort(head);
head2 = mergeSort(head2);
return merge(head, head2);
}
public ListNode merge(ListNode l1, ListNode l2) {
ListNode helper = new ListNode(0);
ListNode runner = helper;
while (l1 != null && l2 != null) {
if (l1.val < l2.val) {
runner.next = l1;
l1 = l1.next;
runner = runner.next;
} else {
runner.next = l2;
l2 = l2.next;
runner = runner.next;
}
if (l1 != null)
runner.next = l1;
if (l2 != null)
runner.next = l2;
}
return helper.next;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
【Leetcode】Sort List (Sorting)的更多相关文章
- 【LeetCode】Sort Colors 解题报告
[题目] Given an array with n objects colored red, white or blue, sort them so that objects of the same ...
- 【Leetcode】Sort List JAVA实现
Sort a linked list in O(n log n) time using constant space complexity. 1.分析 该题主要考查了链接上的合并排序算法. 2.正确代 ...
- 【LeetCode】 sort list 单清单归并
称号:Sort a linked list in O(n log n) time using constant space complexity. 思路:要求时间复杂度O(nlogn) 知识点:归并排 ...
- 【LeetCode】Sort Colors 数组排序
题目:Sort color <span style="font-size:18px;">/*LeetCode sort colors 题目:输入一个数组.包括0,1,2 ...
- 【LeetCode】969. Pancake Sorting 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 模拟法 日期 题目地址:https://leetco ...
- 【LeetCode】Sort Colors
Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...
- 【leetcode】Sort Colors(middle)☆
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- 【leetcode】Sort List (middle)
Sort a linked list in O(n log n) time using constant space complexity. 思路: 用归并排序.设输入链表为S,则先将其拆分为前半部分 ...
- 【leetcode】Sort List
Sort List Sort a linked list in O(n log n) time using constant space complexity. 需要采用归并排序对链表进行操作. ...
随机推荐
- IT忍者神龟之使用 PowerDesigner
1. 启动 PowerDesigner 新建物理数据模型 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdmlweWhk/font/5a6L5L2T/fon ...
- vpdn详细说明
VPDN英文为Virtual Private Dial-up Networks,又称为虚拟专用拨号网,是VPN业务的一种,是基于拨号用户的虚拟专用拨号网业务. 中文名 虚拟专用拨号网业务 外文名 ...
- 【新秀疯狂UML系列】——面向对象的技术
从软质工作开始,我们来到与面向对象的接触,接下来的学习材料似乎已经提到了面向对象,在与她的朋友去一个.所以,我们必须知道她多一点点. 一.何为面向对象? 面向对象(Object Oriented).是 ...
- Scilab 的画图函数(1)
Scilab 的画图函数 plot 函数 最主要的是 plot 函数,与 matlab 中的plot 函数类似. xdata = linspace(1,10,50); ydata = sin(xdat ...
- ArcMap合并之路 -- 该段路合并成一个完整的路
#1: 用 Arctoolbox\Data Management Tools\Generalization\dissolve 工具 #2: Options:dissolve field 项选" ...
- 飞信免费邮件api,飞信界面
大家都知道飞信是能够免费发送短信的,可是飞信又没有官方的接口,所以无法借用移动的官方接口实现短信的免费发送,可是还是有一些破解的接口能够使用的. GET方法: 提交格式 http://66.zzuob ...
- PowerMockito使用详解(转)
一.为什么要使用Mock工具 在做单元测试的时候,我们会发现我们要测试的方法会引用很多外部依赖的对象,比如:(发送邮件,网络通讯,远程服务, 文件系统等等). 而我们没法控制这些外部依赖的对象,为了解 ...
- vijos 1234 口袋的天空
最小生成树kruscal算法 #include<iostream> #include<algorithm> #include<cstring> #define ma ...
- JS学习笔记-OO创建怀疑的对象
问了.工厂介绍,解决重码 前面已经提到,JS中创建对象的方法.不难发现,主要的创建方法中,创建一个对象还算简单,假设创建多个类似的对象的话就会产生大量反复的代码. 解决:工厂模式方法(加入一个专门创建 ...
- stringstream转换CString为string出错
使用stringstream转换CString为string时,调试时发现是CString赋给stringstream没有问题,stringstram赋给string就不行,倒也不是没有赋成功,只是赋 ...