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> ...
随机推荐
- Laravel-2
● php发邮件 参考:https://blog.csdn.net/sinat_37390744/article/details/54667794 ● ajax提交表单时防止csrf攻击 1. 在网页 ...
- linux命令:压缩解压打包工具大集合
目录 (1)zip 压缩.解压缩及归档工具有很多,今天小编就整理几个大家较为常用的. compress gzip bzip2 xz zip tar cpio 一.压缩.解压工具 用法 压缩 工具 压 ...
- RTOS 和中断之间要注意的
#define configLIBRARY_LOWEST_INTERRUPT_PRIORITY 15 #define configLIBRARY_MAX_SYSCALL_INTERRUPT_PRI ...
- [redis] hiredis-vip 简单使用
redis的C语言client,有几个:https://redis.io/clients#c 支持cluster的只有一个唯品会的版本:https://github.com/vipshop/hired ...
- [math] sagemath
官网首页:http://www.sagemath.org 首页里引出的两个教程 http://www.gregorybard.com/Sage.html http://sagebook.gforge. ...
- Python的命令模式和交互模式
Python的命令行模式和交互模式 请注意区分命令行模式和Python交互模式. 在命令行模式下,可以执行python进入Python交互式环境,也可以执行python first.py运行一个.py ...
- 【转】前端框架天下三分:Angular React 和 Vue的比较
前端框架天下三分:Angular React 和 Vue的比较 原文链接:http://blog.csdn.net/haoshidai/article/details/52346865 前端这几年的技 ...
- springcloud第一步:创建eureka注册服务
实现服务注册 创建EureKaserver 项目 Maven依赖 <parent> <groupId>org.springframework.boot</groupId& ...
- iot-web增加apis-namespace组件
1 文件夹复制 apis 2 增加 3 增加module
- asp.net重要小知识
1.服务端用request获取值一般用的是name属性,而ID属性是获取不到值的.对于asp.net中服务器控件一般是把name属性封装的名字和ID相同.