这个问题需要与归并排序排两个名单,基本思路分为切割与合并

合并后的代码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)的更多相关文章

  1. 【LeetCode】Sort Colors 解题报告

    [题目] Given an array with n objects colored red, white or blue, sort them so that objects of the same ...

  2. 【Leetcode】Sort List JAVA实现

    Sort a linked list in O(n log n) time using constant space complexity. 1.分析 该题主要考查了链接上的合并排序算法. 2.正确代 ...

  3. 【LeetCode】 sort list 单清单归并

    称号:Sort a linked list in O(n log n) time using constant space complexity. 思路:要求时间复杂度O(nlogn) 知识点:归并排 ...

  4. 【LeetCode】Sort Colors 数组排序

    题目:Sort color <span style="font-size:18px;">/*LeetCode sort colors 题目:输入一个数组.包括0,1,2 ...

  5. 【LeetCode】969. Pancake Sorting 解题报告(Python & C++)

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

  6. 【LeetCode】Sort Colors

    Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...

  7. 【leetcode】Sort Colors(middle)☆

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  8. 【leetcode】Sort List (middle)

    Sort a linked list in O(n log n) time using constant space complexity. 思路: 用归并排序.设输入链表为S,则先将其拆分为前半部分 ...

  9. 【leetcode】Sort List

    Sort List Sort a linked list in O(n log n) time using constant space complexity.   需要采用归并排序对链表进行操作. ...

随机推荐

  1. java ClassLoader static

    package init; class Person { private static Person person = new Person(); public static int count2 = ...

  2. T-Sql(一)简单语法

    原文:T-Sql(一)简单语法 Sql Server是鄙人学习的第一种数据库,对Sql Server有一种特别的情感,下面就说一下Sql Server的简单语法,适用初学者. 1,创建数据库creat ...

  3. LightOJ1010---Knights in Chessboard (规律题)

    Given an m x n chessboard where you want to place chess knights. You have to find the number of maxi ...

  4. Axis2 -POJO

    POJO,Plain Old Java Object,简单Java物. 通告Webservice 1.书写Hello.java public class Hello { public String s ...

  5. poj 3237 Tree(树链拆分)

    题目链接:poj 3237 Tree 题目大意:给定一棵树,三种操作: CHANGE i v:将i节点权值变为v NEGATE a b:将ab路径上全部节点的权值变为相反数 QUERY a b:查询a ...

  6. cocos2dx3.1-lua移植android流程

    我很懒惰,写这篇博客只是为了能够转出后,当忘记查看,所以我写了下面非常简单的内容.假设完全没有经验的学生请找另一篇文章 一.环境配置(win7): 用户变量如下面: ANDROID_SDK_ROOT: ...

  7. ruby简单的基本 3

    类 Ruby一切都是对象,它包含了一个恒定.例如,可以使用.class物业查看对象的类型,你可以看一下1.class.你会发现常1类型是Fixnum,1但它是Fixnum的一个例子. Ruby本类cl ...

  8. 获取Winform窗体、工作区 宽度、高度、命名空间、菜单栏高度等收集

    MessageBox.Show("当前窗体标题栏高"+(this.Height - this.ClientRectangle.Height).ToString());//当前窗体标 ...

  9. wpf 9张图片的连连看

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...

  10. 算法 《霍纳的方法java实践》

    [历史背景] 霍纳的方法是中国南宋时期的数学家秦九韶表述求解一元高次多项式的值的算法--正负开方术. 它也能够配合牛顿法用来求解一元高次多项式的根.在西方被称作霍纳算法(Horner algorith ...