Sort a linked list in O(n log n) time using constant space complexity.

1、分析

该题主要考查了链接上的合并排序算法。

2、正确代码实现

package com.edu.leetcode;
import com.edu.leetcode.ListNode; public class SortList { /**
* @param args
*/
public ListNode Merge(ListNode first,ListNode second){ //合并两个有序链表,并返回新链表的投结点
ListNode rear;
ListNode head;
rear=head=new ListNode(-1); while(first!=null&&second!=null){
if(first.val<=second.val){
rear.next=first;
rear=first;
first=first.next;
}
else{
rear.next=second;
rear=second;
second=second.next;
}
}
if(first!=null)
rear.next=first;
else
rear.next=second; return head.next;
} public ListNode sortList(ListNode head){
/*
* 实现链表的合并排序:1、将链表划分成基本相等的两个链表
* 2、递归将这两个链接继续划分,直到链表的长度为0或者1为止
* 3、调用Merge()将链接进行合并
*/ if(head==null||head.next==null)
return head;
ListNode mid =head;
ListNode pos =mid.next;
while(pos!=null){
pos=pos.next;
if(pos!=null){
pos=pos.next;
mid=mid.next;
}
}
ListNode q=sortList(mid.next);
mid.next=null;
return Merge(sortList(head), q);
} public static void main(String[] args) {
// TODO Auto-generated method stub
ListNode first1 = new ListNode(0);
ListNode rear1 =first1; for(int i=9;i>=1;i--){
ListNode q= new ListNode(i);
rear1.next=q;
rear1=q;
}
ListNode q=first1;
while(q!=null){
System.out.print(q.val+ ",");
q=q.next;
}
System.out.println();
SortList sl = new SortList();
sl.sortList(first1); ListNode p=first1;
while(p!=null){
System.out.print(p.val+ ",");
p=p.next;
}
System.out.println();
} }

3、有点问题的代码

这里的问题主要是将上面的SortList()方法分成两步实现:Divide()和MergeSort()两部分。个人觉得应该是一样的。但运行结果确实不同,如果大神浏览,请指点一下小弟。。

class ListNode{
int val;
ListNode next=null;
public ListNode(){
}
public ListNode(int val){
this.val=val;
}
} public class SortList { public ListNode Merge(ListNode first,ListNode second){
ListNode rear;
ListNode head;
rear=head=new ListNode(); while(first!=null&&second!=null){
if(first.val<=second.val){
rear.next=first;
rear=first;
first=first.next;
}
else{
rear.next=second;
rear=second;
second=second.next;
}
}
if(first!=null)
rear.next=first;
else
rear.next=second; return head.next;
} public ListNode Divide(ListNode first){ //将一个链表划分成两个基本相等的子链表
if(first==null)
return null;
ListNode mid =first;
ListNode pos =mid.next;
while(pos!=null){
pos=pos.next;
if(pos!=null){
pos=pos.next;
mid=mid.next;
}
}
ListNode q=mid.next;
mid.next=null;
return q;
} public void MergeSort(ListNode first){ //合并排序
if(first!=null&&first.next!=null){
ListNode second =Divide(first); //将链表划分成两部分
MergeSort(first); //递归
MergeSort(second);
Merge(first, second);
} } public static void main(String[] args) {
// TODO Auto-generated method stub
ListNode first1 = new ListNode(0);
ListNode rear1 =first1; for(int i=9;i>=1;i--){
ListNode q= new ListNode(i);
rear1.next=q;
rear1=q;
} ListNode q=first1;
while(q!=null){
System.out.print(q.val+ ",");
q=q.next;
}
System.out.println();
SortList sl = new SortList();
sl.MergeSort(first1); ListNode p=first1;
while(p!=null){
System.out.print(p.val+ ",");
p=p.next;
}
System.out.println(); } }

【Leetcode】Sort List JAVA实现的更多相关文章

  1. LeetCode: Sort Colors 解题报告

    Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...

  2. LeetCode—-Sort List

    LeetCode--Sort List Question Sort a linked list in O(n log n) time using constant space complexity. ...

  3. leetcode 148. Sort List ----- java

    Sort a linked list in O(n log n) time using constant space complexity. 排序,要求是O(nlog(n))的时间复杂度和常数的空间复 ...

  4. leetcode 147. Insertion Sort List ----- java

    Sort a linked list using insertion sort. 插入排序. /** * Definition for singly-linked list. * public cla ...

  5. [LeetCode] Sort List 链表排序

    Sort a linked list in O(n log n) time using constant space complexity. 常见排序方法有很多,插入排序,选择排序,堆排序,快速排序, ...

  6. LeetCode: Sort List 解题报告

    Sort List Sort a linked list in O(n log n) time using constant space complexity. 使用Merge Sort, 空间复杂度 ...

  7. [LeetCode] Sort Characters By Frequency 根据字符出现频率排序

    Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: ...

  8. [LeetCode] Sort Colors 颜色排序

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

  9. leetcode 日记 4sum java

    整体思路同之前的一样,依然采取降低维度的方式进行 public List<List<Integer>> solution(int nums[], int target) { L ...

随机推荐

  1. stringbuffer与stringbuilder的区别?

    1. 在执行速度方面的比较:StringBuilder > StringBuffer 2. StringBuffer与StringBuilder,他们是字符串变量,是可改变的对象,每当我们用它们 ...

  2. iOS 开发--动画

    在iOS开发中,制作动画效果是最让开发者享受的环节之一.一个设计严谨.精细的动画效果能给用户耳目一新的效果,吸引他们的眼光 —— 这对于app而言是非常重要的.我们总是追求更为酷炫的实现,如果足够仔细 ...

  3. PHP页面跳转几种实现技巧

    PHP被许多程序员用来开发WEB的首选语言.在实际开发中,网站的各项功能都可以通过PHP语言的编写来满足,比如PHP页面跳转这一方法. 探讨PHP变量解析顺序如何获取提交数据 深入解读PHP运行机制 ...

  4. dwz ie10一直提示数据加载中

    dwz js资源jquery.validate.js 搜索 this.attr('novalidate', 'novalidate'); 在33行左右 用if (typeof (Worker) !== ...

  5. [转]“WARNING: soft rlimits too low” in MongoDB with Mac OS X

    转自:Programming and Technology If you get this warning when you connect to mongo shell in Mac OX X: * ...

  6. java post 请求

    新公司的分词为post调用方式,以前还没用过post,这次上网查了下,比较简单,但还是写篇博客记录下,代码为网上找的,非原创. package com.chuntent.tool; import ja ...

  7. C# 数组、一维数组、二维数组、多维数组、锯齿数组

    C#  数组.一维数组.二维数组.多维数组.锯齿数组 一.数组: 如果需要使用同一类型的对象,就可以使用数组,数组是一种数据结构,它可以包含同一类型的多个元素.它的长度是固定的,如长度未知的情况下,请 ...

  8. asp.net MVC3 “System.Web.Mvc.ModelClientValidationRule”问题

    错误提示: Error 1 The type 'System.Web.Mvc.ModelClientValidationRule' exists in both 'c:\Program Files ( ...

  9. js中substr与substring的用法与区别

    substrsubstr(start,length)表示从start位置开始,截取length长度的字符串. var src="images/pic_1.png";alert(sr ...

  10. Java中什么时候使用构造方法

    JAVA是面向对象的语言,面向对象不是这么直接简单,它的设计思想就是要代码重用.即我以前干过类似的事,那么我找出以前可以用到的代码,完成一部分.以前没有的我重新写.这样就有了类.有了类,就是有了可以重 ...