【Leetcode】Sort List JAVA实现
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实现的更多相关文章
- LeetCode: Sort Colors 解题报告
Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...
- LeetCode—-Sort List
LeetCode--Sort List Question Sort a linked list in O(n log n) time using constant space complexity. ...
- leetcode 148. Sort List ----- java
Sort a linked list in O(n log n) time using constant space complexity. 排序,要求是O(nlog(n))的时间复杂度和常数的空间复 ...
- leetcode 147. Insertion Sort List ----- java
Sort a linked list using insertion sort. 插入排序. /** * Definition for singly-linked list. * public cla ...
- [LeetCode] Sort List 链表排序
Sort a linked list in O(n log n) time using constant space complexity. 常见排序方法有很多,插入排序,选择排序,堆排序,快速排序, ...
- LeetCode: Sort List 解题报告
Sort List Sort a linked list in O(n log n) time using constant space complexity. 使用Merge Sort, 空间复杂度 ...
- [LeetCode] Sort Characters By Frequency 根据字符出现频率排序
Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: ...
- [LeetCode] Sort Colors 颜色排序
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- leetcode 日记 4sum java
整体思路同之前的一样,依然采取降低维度的方式进行 public List<List<Integer>> solution(int nums[], int target) { L ...
随机推荐
- iOS开发cell--滑动手势显示按钮
// 主要代码 #warning iOS8 - #pragma mark 在滑动手势删除某一行的时候,显示出更多的按钮 - (NSArray *)tableView:(UITableView *)ta ...
- SPRING IN ACTION 第4版笔记-第十一章Persisting data with object-relational mapping-002设置JPA的EntityManagerFactory(<persistence-unit>、<jee:jndi-lookup>)
一.EntityManagerFactory的种类 1.The JPA specification defines two kinds of entity managers: Applicatio ...
- 在Ogre中加载自己的资源包
转自:http://www.cnblogs.com/minggoddess/archive/2011/02/19/1958472.html 由于数据保护的需要,一款游戏一般都会有自己独有的资源包,这样 ...
- http://www.ruanyifeng.com/blog/2007/03/metadata.html
http://www.ruanyifeng.com/blog/2007/03/metadata.html
- Sql server cast(as nvarchar) 默认长度问题
Sql server 在我的SQL语句中:sql=".........cast(ziduan as nvarchar) ..............." 这样之后,ziduan被转 ...
- 第三篇 从EXCEL电子表格到数据库
一个靠EXCEL电子表格处理各部门业务数据的公司和一个使用一个统一的数据库存储各个部门用到的业务数据并提供大量权限不同的使用界面给用户的公司两者有什么不同呢? EXCEL电子表格是数据和操纵数据的 ...
- Linux同步机制 - 多线程开发总结
1 对于CPU开销大的场景,能利用多核,就尽量利用多核(常常自以为某需求的运算量不大,且CPU足够快,就偷懒写个单线程,结果效率很低) 2 使用多线程的时候,默认是加锁的.在加锁保证业务正常的条件下, ...
- BigDecimal四舍五入
/*由数字字符串构造BigDecimal的方法 *设置BigDecimal的小数位数的方法 */ import java.math.BigDecimal; //数字字符串 String StrBd=& ...
- 基于jQuery的日历插件
上个星期看到同事做一个有关日历提醒功能的需求,为了找个插件也是费了不少心思,然后刚好有时间就试着写了一个简单demo 来看下最终效果图吧: 是长得丑了一点,不要吐槽我-.- 首先来说说这个日历主要的制 ...
- java中进程与线程的三种实现方式
一:进程与线程 概述:几乎任何的操作系统都支持运行多个任务,通常一个任务就是一个程序,而一个程序就是一个进程.当一个进程运行时,内部可能包括多个顺序执行流,每个顺序执行流就是一个线程. 进程:进程是指 ...