Sort List (Frequent++)
来源:互联网 发布:linux find mtime时间 编辑:程序博客网 时间:2024/06/07 22:15
http://www.lintcode.com/en/problem/sort-list/
题目:将链表排序,使得时间复杂度为O(nlogn)
解答:
方法一:归并排序
public class Solution {
/**
* @param head: The head of linked list.
* @return: You should return the head of the sorted linked list,
using constant space complexity.
*/
public ListNode sortList(ListNode head) {
// write your code here
if (head == null || head.next == null) {
return head;
}
ListNode mid = middle(head);
ListNode right = sortList(mid.next);
mid.next = null;
ListNode left = sortList(head);
return merge(left, right);
}
private ListNode middle(ListNode head) {
ListNode fast = head.next;
ListNode slow = head;
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
}
return slow;
}
private ListNode merge(ListNode head1, ListNode head2) {
ListNode dummy = new ListNode(0);
ListNode head = dummy;
while (head1 != null && head2 != null) {
if (head1.val < head2.val) {
head.next = head1;
head = head.next;
head1 = head1.next;
} else {
head.next = head2;
head = head.next;
head2 = head.next;
}
}
while (head1 != null) {
head.next = head1;
head = head.next;
head1 = head1.next;
}
while (head2 != null) {
head.next = head2;
head = head.next;
head2 = head.next;
}
return dummy.next;
}
}
方法二:快速排序
public class Solution {
/**
* @param head: The head of linked list.
* @return: You should return the head of the sorted linked list,
using constant space complexity.
*/
public ListNode sortList(ListNode head) {
// write your code here
if (head == null || head.next == null) {
return head;
}
ListNode leftDummy = new ListNode(0), leftHead = leftDummy;
ListNode rightDummy = new ListNode(0), rightHead = rightDummy;
ListNode middleDummy = new ListNode(0), middleHead = middleDummy;
ListNode mid = middle(head);
while (head != null) {
if (head.val > mid.val) {
rightHead.next = head;
rightHead = rightHead.next;
} else if (head.val < mid.val) {
leftHead.next = head;
leftHead = leftHead.next;
} else {
middleHead.next = head;
middleHead = middleHead.next;
}
head = head.next;
}
rightHead.next = null;
leftHead.next = null;
middleHead.next = null;
ListNode left = sortList(leftDummy.next);
ListNode right = sortList(rightDummy.next);
return merge(left, middleDummy.next, right);
}
private ListNode middle(ListNode head) {
ListNode fast = head.next;
ListNode slow = head;
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
}
return slow;
}
private ListNode merge(ListNode head1, ListNode head2, ListNode head3) {
ListNode tail1 = head1;
ListNode tail2 = head2;
while (tail2.next != null) {
tail2 = tail2.next;
}
if (head1 != null){
while (tail1.next != null) {
tail1 = tail1.next;
}
tail1.next = head2;
tail2.next = head3;
return head1;
} else {
tail2.next = head3;
return head2;
}
}
}
- Sort List (Frequent++)
- list sort
- list sort
- Sort List
- Sort List
- list - sort
- Sort List
- Sort List
- Sort List
- Sort List
- Sort List
- Sort List
- Sort List
- Sort List
- Sort List
- Sort List
- Sort List
- Sort List
- android 在多线程中运用的Toast
- 数据库的基本操作
- Androidstudio使用笔记
- 查看jdk的版本以及路径
- jQuey在MyEclipse中不起作用的解决方法
- Sort List (Frequent++)
- 简单抓站的N种方式-urllib与bs4
- 通过GlobalMemoryStatus函数获取当前虚拟内存和物理内存信息
- LeetCode 171. Excel Sheet Column Number
- 回顾大一
- 几种USB控制器类型:OHCI,UHCI,EHCI,XHCI
- Bash语言总结
- android-ndk-r14b\RELEASE.TXT 文件获取 NDK 版本失败
- spring 文件上传与下载【idea】