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. Hadoop基础教程之分布式环境搭建

    前面,我们已经在单机上把Hadoop运行起来了,但我们知道Hadoop支持分布式的,而它的优点就是在分布上突出的,所以我们得搭个环境模拟一下. 在这里,我们采用这样的策略来模拟环境,我们使用3台ubu ...

  2. ios开发--GCD使用介绍:4-延迟执行操作

    在开发过程中,我们有时会希望把一些操作封装起来延迟一段时间后再执行.iOS开发中,有两种常用的方法可以实现延迟执行,一种是使用GCD,另外一种是使用NSRunLoop类中提供的方法. 1.使用GCD实 ...

  3. 使用RedisTemplate的操作类访问Redis(转)

    深入理解Spring Redis的使用 (三).使用RedisTemplate的操作类访问Redis 事务需要开启enableTransactionSupport,然后使用@transactional ...

  4. Centos下修改启动项和网络配置

    1.Centos默认是从图形界面启动,需要较多的资源,为了节省资源可以从命令行启动.修改方法如下: /etc/inittab文件,把 代码: id:5:initdefault:这一行,修改成 代码: ...

  5. 【CCS仿真】用matlab把CCS保存的32位16进制的数据转换为十进制的数

    2013-12-04 16:37:28 使用fscanf函数即可完成. 例如,CCS保存的.dat文件Copy_of_forward_i_f.dat如下: 1651 1 81008800 0 4000 ...

  6. sql2000无法打开1433端口及解决方法

    1.如果你是win2003,那么一定要安装sql的补丁sp3a以上版本SP 检查你的SQL有没有打补丁,没有的话要打上补丁,检查的方法是在查询分析器中运行:select @@version如果出来的版 ...

  7. IP地址的定义和含义

    IP的定义 ip 是32位无符号整数,最小,最大分别是- 0.0.0.0 - 255.255.255.255 具体来说,由一个ip由 Net-ID+Host-ID 两部分组成,Net-ID 相同,那么 ...

  8. 设备模型之kobject,kset及其关系

    Linux2.6以后的设备驱动,都是在设备模型的基础上构建的,因此,要编写linux下的设备驱动程序,不论是usb设备,pci设备等,都需要了解设备模型. 设备模型的基础结构体主要是kobject,k ...

  9. cdoj 1324 卿学姐与公主 线段树裸题

    卿学姐与公主 Time Limit: 2000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit St ...

  10. non-overlapping-intervals

    https://leetcode.com/problems/non-overlapping-intervals/ 其中还用到了Java的Comparator接口和其中的compare方法. packa ...