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) ...
随机推荐
- linux 的有用的网站
从windows下移到linux下还有很长的路走阿,慢慢记录一些有用的网站吧 http://www.yolinux.com/ http://linux.die.net/
- TMF SID中的角色模式
角色模式 Copyright © TeleManagement Forum 2013. All Rights Reserved. This document and translations of i ...
- PS2018学习笔记(30-35节)
30-35:万能的钢笔-制图抠图必学-part(1-6) # 本节知识点: 钢笔工具 贝塞尔曲线 绘图方式 光标状态认识 路径 形状 形状工具 矢量蒙版 # 本节段落表: 钢笔工具知识 直线绘制知识 ...
- Neutron网络研究
你将学到什么 虚拟机的Ping包是如何出外网的 DevStack环境准备 节点 硬件配置 网络配置 类型 操作系统 DevStack 4G 2CPU 50GB 2张网卡(NAT模式) VMWare虚拟 ...
- centos lamp 配置
# 1. 查看是否有httpd进程正在运行(下图是有的情况) ps -ef|grep httpd
- Ubuntu常用命令集合
文件操作 查看当前目录: pwd 参考文章:https://blog.csdn.net/qq_33421080/article/details/76551554 应用编辑类 安装: sudo apt- ...
- HDP3.1 中 YRAN 和 MR2 的内存大小配置的计算方式
Container 是 YARN 中基本的处理单元,它是对内存.CPU等计算的封装.总的来说,每个core每块硬盘 分配2个 container,能获得较好的集群利用率. 1. 确定可用内存大小. 对 ...
- MCP|XHK|High-density peptide arrays help to identify linear immunogenic B cell epitopes in individuals naturally exposed to malaria infection(高密度肽段阵列有助于在自然暴露于疟疾感染的个体中识别线性免疫原性B细胞表位)
文献名:High-density peptide arrays help to identify linear immunogenic B cell epitopes in individuals n ...
- centos 7 安装python3
centos系统默认已安装python2.7,python3需要手动安装.以上是安装步骤 一.备份原来的2.7版本 首先看一下默认的python2.7在哪里 [root@apple ~]# cd / ...
- E - Palindrome Numbers
题目链接:https://vjudge.net/contest/237394#problem/E A palindrome is a word, number, or phrase that read ...