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. 通过GeoIP2分析访问者IP获取地理位置信息

    原文链接:http://blog.csdn.net/johnnycode/article/details/42028841 MaxMind GeoIP2 服务能识别互联网用户的地点位置与其他特征,应用 ...

  2. android-exploitme(八):内存保护

    如果一个手机被锁屏了,但是有个app还在后台运行,这个时候你想知道些app的信息,需要分析他的内存状态. 1. 首先运行模拟器,打开emm,使得模拟器返回锁屏状态 2. 打开ddms,下载内存文件

  3. mysql中的连接

    SQL join 用于根据两个或多个表中的列之间的关系,从这些表中查询数据. join可以分为内连接和外连接,外连接分为左连接.右连接和全连接 现有两个表 员工表和部门表 员工表 部门表 1.内连接( ...

  4. Linux 添加环境变量和删除环境变量

    环境变量是一个具有特定名字的对象,它包含了一个或者多个应用程序所将使用到的信息.例如PATH.在交叉编译中,会经常运用到环境变量的设置. 在linux中,查看当前全部的环境变量的命令式env. 当然也 ...

  5. 汉字编码:GB2312, GBK, GB18030, Big5

    前一篇博文:ANSI是什么编码?中有这样一段小故事: 话说计算机是由美国佬搞出来的嘛,他们觉得一个字节(可以表示256个编码)表示英语世界里所有的字母.数字和常用特殊符号已经绰绰有余了(其实ASCII ...

  6. [NYIST15]括号匹配(二)(区间dp)

    题目链接:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=15 经典区间dp,首先枚举区间的大小和该区间的左边界,这时右边界也可计算出来.首先初 ...

  7. CMS 垃圾回收日志

    CMS 垃圾回收日志 https://blogs.oracle.com/poonam/entry/understanding_cms_gc_logs http://www.blogjava.net/D ...

  8. HibernateTools的使用

    1. 到 Hibernate.org官网上 下载最新版的 Hibernate Tools,我用的是 HibernateTools-3.2.4.GA版 2. 将 下载下来的压缩包解压缩,里面会有 plu ...

  9. Post的请求案例

    1.简单的post请求案例 $.post(rootPath+"/jasframework/loginLog/getStatisticsInfoByUserId.do",functi ...

  10. git workflow常用命令

    git init git status git add readme.txt git add --all         Adds all new or modified files git comm ...