LeetCode Sort List 链表排序(规定 O(nlogn) )
Status: Accepted
Runtime: 66 ms
题意:根据给出的单链表,用O(nlogn)的时间复杂度来排序。由时间复杂度想到快排、归并这两种排序。本次用的是归并排序。递归将链表的规模不断二分到只剩下1或2个元素为止,这也是递归出口,一旦出现这两种情况就可以返回。这里有个问题,链表也能二分?可以的,只是麻烦了些,用两个指针可以实现找到中点。本次代码没有详细分析具体的复杂度,但确实是归并。
注意:要考虑空链表,单个元素的链表,以及多个元素的链表。
LeetCode的代码:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *sorted(ListNode *head) {
//递归出口
if(!head->next) return head;
if(!head->next->next) //只有两个结点时就可以排序并返回了
{
if(head->val > head->next->val )
{
head->next->next=head;
head=head->next;
head->next->next=;
}
return head;
} //找中点。方法:p2每往后移2步,p1就移1步,当p2到达最后一个结点时,p1刚好在中间
ListNode *p1=head,*p2=head;
while(p2)
{
if(p2->next)
{
p2=p2->next->next; //允许p2为空,但若p2->next为空,则p2->next->next会出错
p1=p1->next;
}
else break;
}
ListNode * sec=p1->next; //左一半的最后一个结点的next必须为空,此举是必须的
p1->next=; //二分递归
ListNode * temp1=sorted(head);
ListNode * temp2=sorted(sec); //归并,且返回
ListNode * sorted_head=NULL,*sorted_rear=NULL,*jus=NULL;
if(temp1->val < temp2->val ) //初始化链表头尾
{
sorted_head=sorted_rear=temp1;
temp1=temp1->next;
jus=temp1; //jus摆在刚减少元素的那个链表,以方便判断是否该链表已没有元素了
}
else
{
sorted_head=sorted_rear=temp2;
temp2=temp2->next;
jus=temp2;
}
while(jus) //两个链表都非空
{
if(temp1->val < temp2->val)
{
sorted_rear->next=temp1;
temp1=temp1->next;
jus=temp1;
}
else
{
sorted_rear->next=temp2;
temp2=temp2->next;
jus=temp2;
}
sorted_rear=sorted_rear->next;
}
if(!temp1) //temp1为空
sorted_rear->next=temp2;
else
sorted_rear->next=temp1;
return sorted_head;
} ListNode *sortList(ListNode *head) {
if(!head) return ;
if(head->next==NULL) return head;
return sorted(head);
}
};
Sort list
可自己测试的代码:
#include <iostream>
using namespace std; struct ListNode {
int val;
ListNode *next;
}; ListNode *sorted(ListNode *head) {
//递归出口
if(!head->next) return head;
if(!head->next->next) //只有两个结点时就可以排序并返回了
{
if(head->val > head->next->val )
{
head->next->next=head;
head=head->next;
head->next->next=;
}
return head;
} //找中点。方法:p2每往后移2步,p1就移1步,当p2到达最后一个结点时,p1刚好在中间
ListNode *p1=head,*p2=head;
while(p2)
{
if(p2->next)
{
p2=p2->next->next; //允许p2为空,但若p2->next为空,则p2->next->next会出错
p1=p1->next;
}
else break;
}
ListNode * sec=p1->next; //左一半的最后一个结点的next必须为空,此举是必须的
p1->next=; //二分递归
ListNode * temp1=sorted(head);
ListNode * temp2=sorted(sec); //归并,且返回
ListNode * sorted_head=NULL,*sorted_rear=NULL,*jus=NULL;
if(temp1->val < temp2->val ) //初始化链表头尾
{
sorted_head=sorted_rear=temp1;
temp1=temp1->next;
jus=temp1; //jus摆在刚减少元素的那个链表,以方便判断是否该链表已没有元素了
}
else
{
sorted_head=sorted_rear=temp2;
temp2=temp2->next;
jus=temp2;
}
while(jus) //两个链表都非空
{
if(temp1->val < temp2->val)
{
sorted_rear->next=temp1;
temp1=temp1->next;
jus=temp1;
}
else
{
sorted_rear->next=temp2;
temp2=temp2->next;
jus=temp2;
}
sorted_rear=sorted_rear->next;
}
if(!temp1) //temp1为空
sorted_rear->next=temp2;
else
sorted_rear->next=temp1;
return sorted_head;
} ListNode *sortList(ListNode *head) {
if(!head) return ;
if(head->next==NULL) return head;
return sorted(head);
} int main()
{ ListNode * head=new(ListNode);
head->val=; //链表有1个元素
head->next=NULL;
ListNode * p=head;
ListNode * temp=NULL; //int a[11]={2,1,3,6,5,8,4,9,7,10};
int a[]={,}; //元素自己随意添加
for(int i=;i<;i++) //在这里控制元素个数。
{
temp=new(ListNode);
temp->val=a[i];
temp->next=NULL;
p->next=temp;
p=p->next;
}
p=sortList(head);
while(p)
{
printf("%d\n",p->val);
p=p->next;
} return ;
}
Test Code
LeetCode Sort List 链表排序(规定 O(nlogn) )的更多相关文章
- [LeetCode] Sort List 链表排序
Sort a linked list in O(n log n) time using constant space complexity. 常见排序方法有很多,插入排序,选择排序,堆排序,快速排序, ...
- [LeetCode] 148. Sort List 链表排序
Sort a linked list in O(n log n) time using constant space complexity. Example 1: Input: 4->2-> ...
- [LeetCode]147. Insertion Sort List链表排序
插入排序的基本思想 把排好的放在一个新的变量中,每次拿出新的,排进去 这个新的变量要有超前节点,因为第一个节点可能会有变动 public ListNode insertionSortList(List ...
- [LeetCode] Sort Colors 颜色排序
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- [LintCode] Sort List 链表排序
Sort a linked list in O(n log n) time using constant space complexity. Have you met this question in ...
- LeetCode::Sort List 具体分析
Sort a linked list in O(n log n) time using constant space complexity. 这道题目非常简短的一句话.给链表排序,看到nlogn.我们 ...
- sort 树 hash 排序
STL 中 sort 函数用法简介 做 ACM 题的时候,排序是一种经常要用到的操作.如果每次都自己写个冒泡之类的 O(n^2) 排序,不但程序容易超时,而且浪费宝贵的比赛时间,还很有可能写错. ST ...
- Leetcode:148_Sort List | O(nlogn)链表排序 | Medium
题目:Sort List Sort a linked list in O(n log n) time using constant space complexity 看题目有两个要求:1)时间复杂度为 ...
- 148. Sort List (java 给单链表排序)
题目:Sort a linked list in O(n log n) time using constant space complexity. 分析:给单链表排序,要求时间复杂度是O(nlogn) ...
随机推荐
- @Html.AntiForgeryToken() 源码分析,表单防伪码的生成
源码来自MVC4@Html.AntiForgeryToken() 源码分析 public MvcHtmlString AntiForgeryToken() { return new MvcHtmlSt ...
- 开发一个android项目后的总结
首先是自己在OneNote上面记录了一些流水: 个人感觉这一路开发下来,学到了一些知识,也碰到了许许多多的问题,也解决了一些问题.总体来看,有几点(个人观点,不支持任何讨论): 1.Java是很优秀的 ...
- unique within an element
从tomcat 6 升到 tomcat-7.0.12 jsp页面报: org.apache.jasper.JasperException: /XXX/XXX.jsp(59,55) Attribute ...
- git 提交解决本地与远程冲突
首先介绍一下背景. 如果有一个工程A,开始时test.txt 的内容如下 chenfool hello world 作者通过 git clone 的方式,将这个项目download 到本地. 此时,作 ...
- Stream流、方法引用
Stream流.方法引用 Stream流.方法引用 Stream流.方法引用 Stream流.方法引用 Stream流.方法引用 ... ...
- POJ2388-Who's in the Middle
题目链接:点击打开链接 Who's in the Middle Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45683 ...
- python抓取知乎热榜
知乎热榜讨论话题,https://www.zhihu.com/hot,本文用python抓取下来分析 #!/usr/bin/python # -*- coding: UTF-8 -*- from ur ...
- JMeter - 连续性能测试 - JMeter + ANT + Jenkins集成 - 第2部分
目标: 创建包含性能测试流程的持续交付管道,以尽早检测任何与性能相关的问题. 通常,全面的性能测试将在分段/预生产环境中完成,该环境可能与您的生产环境相同.在完成QA功能/回归验证后,将代码推送到分段 ...
- input和button不同高 和 rem
rem的使用 浏览器中默认的字体大小是 16px,此时为 100%.当我们在使用 rem 的时候,常常给 html设置为 html{font-size:62.5;},这样的好处是 1rem 刚好为 1 ...
- Markdown 简单标签语言
Markdownhttps://www.cnblogs.com/ixysy/p/6236782.html Markdown基本语法https://www.jianshu.com/p/191d1e21f ...