leecode第一百四十八题(排序链表)

class Solution {
public:
void sort_list(ListNode* head1, ListNode* head2,int len)//在原链表上进行排序
{
ListNode* cur_node1 = head1;
ListNode* cur_node2 = head1;
while (cur_node2->next != head2)
cur_node2 = cur_node2->next;
if (cur_node1->val > cur_node2->next->val)//必须先让cur_node1->val小于head2-》val
{
int temp = cur_node1->val;
cur_node1->val = cur_node2->next->val;
cur_node2->next->val = temp;
}
while (len > )
{
while (cur_node1!= cur_node2->next && cur_node1->next->val < cur_node2->next->val)
cur_node1 = cur_node1->next;
if (cur_node1 == cur_node2->next)//说明head2的链表都小于head1
return;
else if(cur_node1 == cur_node2)//说明cur_node2->next后面没有统计,但是前面的都满足了
{
cur_node2 = cur_node2->next;
len--;
}
else//要交换了
{
ListNode* temp = cur_node2->next;
cur_node2->next = cur_node2->next->next;
temp->next = cur_node1->next;
cur_node1->next = temp;
len--;
}
}
}
ListNode* sort_List(ListNode* head, int len)//归并排序
{
if (len == )
return NULL;
if (len == )
return head;
ListNode* mid_node = head;
for (int i = len / ; i > ; i--)
mid_node = mid_node->next;
ListNode* left = sort_List(head, len / );
ListNode* right;
if (len & == )
{
right = sort_List(mid_node, len / + );
sort_list(left, right, len / + );
}
else
{
right = sort_List(mid_node, len / );
sort_list(left, right, len / );
}
return left;
}
ListNode* sortList(ListNode* head) {//初试输入
int len = ;
ListNode* cur_node = head;
while (cur_node != NULL)
{
len++;
cur_node = cur_node->next;
}
ListNode* res = sort_List(head, len);
return res;
}
};
分析:
为了满足时间复杂度,想到归并排序,为了满足空间复杂度,想到在原链表上进行排序。
但是在原链表上进行排序碰到问题有点多,尤其是不知道怎么判断终止条件和什么时候交换。
睡了一觉就想出来了。
时间击败63%,空间击败72%,室友说会不会是一晚上换了案例。。。。
说实话我还有点懵懂。
leecode第一百四十八题(排序链表)的更多相关文章
- leecode第一百四十二题(环形链表II)
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...
- leecode第一百四十六题(LRU缓存机制)
class LRUCache { private: unordered_map<int, list<pair<int,int>>::iterator> _m; // ...
- leecode第七十八题(子集)
class Solution { public: vector<vector<int>> subsets(vector<int>& nums) { vect ...
- 【leetcode 简单】 第一百四十六题 最长和谐子序列
和谐数组是指一个数组里元素的最大值和最小值之间的差别正好是1. 现在,给定一个整数数组,你需要在所有可能的子序列中找到最长的和谐子序列的长度. 示例 1: 输入: [1,3,2,2,5,2,3,7] ...
- 第一百四十八节,封装库--JavaScript,菜单切换
第一百四十八节,封装库--JavaScript,菜单切换 首先在封装库封装点击切换方法 /** dian_ji_qie_huan()方法,设置点击切换,将元素设置成点击切换,也就是点击目标元素后,循环 ...
- leecode第一百四十一题(环形链表)
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...
- leecode第八十八题(合并两个有序数组)
class Solution { public: void merge(vector<int>& nums1, int m, vector<int>& nums ...
- 《剑指offer》第十八题(删除链表中重复的结点)
// 面试题18(二):删除链表中重复的结点 // 题目:在一个排序的链表中,如何删除重复的结点?例如,在图3.4(a)中重复 // 结点被删除之后,链表如图3.4(b)所示. #include &l ...
- 《剑指offer》第十八题(在O(1)时间删除链表结点)
// 面试题18(一):在O(1)时间删除链表结点 // 题目:给定单向链表的头指针和一个结点指针,定义一个函数在O(1)时间删除该 // 结点. #include <iostream> ...
随机推荐
- HotSpot虚拟机
注:如其中有不懂的名词,下面有名词解释 1.对象的创建(限于普通Java对象,不包括数组和Class对象等) (1)检查这个指令的参数能否在常量池中定位到一个类的符号引用,并检查这个符号引用代表的类是 ...
- RNN Train和Test Mismatch
李宏毅深度学习 https://www.bilibili.com/video/av9770302/?p=8 在看RNN的时候,你是不是也会觉得有些奇怪, Train的过程中, 是把训练集中的结果作为下 ...
- SQL两张表筛选相同数据和不同数据
原文链接:http://www.cnblogs.com/onesmail/p/6148979.html 方法一: select distinct A.ID from A where A.ID not ...
- cestos7安装zookeeper
zookeeper安装包下载地址http://archive.apache.org/dist/zookeeper/ 一.单机 在/usr目录下 curl -L-O http://archive.apa ...
- Hibernate 补充 ManyToOne、OneToMany、OneToOne的使用例
1.前言 Hibernate 为程序员提供一种级联操作,在编写程序时,通过 Hibernate 的级联功能可以很方便的操作数据库的主从表的数据, 我们最常用的级联是级联保存和级联删除. ...
- Navicat 远程连接 Oracle11g 数据库报错 No listener 的问题
1.首先确认已经启动 OracleOraDb11g_home1TNSListener 服务时,仍无法连接: 2.进入计算机系统属性中查看 Oracle 服务端计算机的全名: 3.进入 Orac ...
- CSS中list-style详解
取消默认的圆点和序号可以这样写list-style:none;, list的属性如下: list-style-type:square;//正方形 list-style-position:inside; ...
- linux----------安装Supervisor是用Python开发的一套通用的进程管理程序
1.linux环境必须安装 python 2.yum install python-setuptools 3.获取supervisor包 wget https://pypi.python.org/pa ...
- laravel----------laravel5.3调度任务以及Artisan
1.在使用的过程中会遇到有些函数不能用需要在php配置文件里面打开: disable_functions = exec,passthru,popen,proc_open,shell_exec,syst ...
- [macOS] PHP双版本,5.6跟7.1
转过来的,原文看这里,https://www.symfony.fi/page/how-to-run-both-php-5-6-and-php-7-x-with-homebrew-on-os-x-wit ...