Leetcode SortList
Sort a linked list in O(n log n) time using constant space complexity.
本题利用归并排序即可
归并排序的核心是将两部分合成一部分,
故开始要将链表分成两部分,利用快慢两个指针,当快指针跑到链表尾部时,慢指针恰好在中间,故可以将链表分成两部分
然后将两个链表合并,注意可以新建一个新节点,作为链表头结点,利用new新建节点后,要注意用delete删除节点,保持良好的编程习惯
#include <iostream>
#include <vector> using namespace std; struct ListNode{
int val;
ListNode *next;
ListNode(int x):val(x), next(NULL){}
}; void printList(ListNode* head){
while(head!=NULL){
cout<<"->"<<head->val;
head = head->next;
}
cout<<endl;
} ListNode *merge(ListNode *head1, ListNode *head2){
// cout<<"======"<<endl;
// printList(head1);
// printList(head2);
// cout<<"----->"<<endl;
if(head1 == NULL ) return head2;
if(head2 == NULL) return head1;
ListNode *mergeHead = new ListNode(-);
ListNode *pre = mergeHead;
mergeHead->next = head1;
while(head1!=NULL && head2 != NULL){
if(head1->val < head2->val) head1= head1->next;
else{
ListNode *node = head2->next;
head2->next = pre->next;
pre->next = head2;
head2 = node;
}
pre = pre->next;
}
if(head2!=NULL) pre->next = head;
ListNode *res = mergeHead->next;
delete mergeHead;
return res;
} ListNode *mergeSort(ListNode *head){
if(head == NULL || head->next == NULL) return head;
ListNode *slow = head;
ListNode *fast = head;
while(fast->next != NULL && fast->next->next != NULL){
slow = slow->next;
fast = fast->next->next;
}
ListNode* head2 = slow->next;
slow->next = NULL;
ListNode* head1 = head;
head1 = mergeSort(head1);
head2 = mergeSort(head2);
return merge(head1,head2);
} ListNode *sortList(ListNode *head){
return mergeSort(head);
} int main(){
ListNode* head = new ListNode();
ListNode* node1 = new ListNode();
ListNode* node2 = new ListNode();
head->next = node1;
node1->next = node2;
ListNode *a = sortList(head);
cout<<"ans:"<<endl;
printList(a);
}
本题更复杂一点的是
给出两个无序链表,然后将其合并,
首先要做的事将无序链表排序,然后将两个有序链表合并
Leetcode SortList的更多相关文章
- leetcode: sortlist之四种方法
原题链接:https://oj.leetcode.com/problems/sort-list/ 题目:空间复杂度为常数,时间复杂度为O(nlogn)的排序链表实现 方法一:第一想法是模拟数组的快速排 ...
- sort-list leetcode C++
Sort a linked list in O(n log n) time using constant space complexity. C++ /** * Definition for sing ...
- [LeetCode] Sort List 链表排序
Sort a linked list in O(n log n) time using constant space complexity. 常见排序方法有很多,插入排序,选择排序,堆排序,快速排序, ...
- leetcode笔记
82. Remove Duplicates from Sorted List II https://leetcode.com/problems/remove-duplicates-from-sorte ...
- leetcode算法分类
利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...
- leetcode bugfree note
463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...
- LeetCode题目分类
利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- 【Leetcode】Sort List JAVA实现
Sort a linked list in O(n log n) time using constant space complexity. 1.分析 该题主要考查了链接上的合并排序算法. 2.正确代 ...
随机推荐
- JQ 全选设定与设置选中
复选数据框 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w ...
- linux文件描述符open file descriptors与open files的区别
一个文件被打开,也可能没有文件描述符,比如current working diretories,memory mapped files and executable text files ;losf可 ...
- tornado web高级开发项目之抽屉官网的页面登陆验证、form验证、点赞、评论、文章分页处理、发送邮箱验证码、登陆验证码、注册、发布文章、上传图片
本博文将一步步带领你实现抽屉官网的各种功能:包括登陆.注册.发送邮箱验证码.登陆验证码.页面登陆验证.发布文章.上传图片.form验证.点赞.评论.文章分页处理以及基于tornado的后端和ajax的 ...
- 无废话ExtJs 入门教程七[登陆窗体Demo:Login]
无废话ExtJs 入门教程七[登陆窗体Demo:Login] extjs技术交流,欢迎加群(201926085) 在这节我们通过前几节讲的内容做一个登陆页面,把前几节讲的内容贯穿一下. 1.代码如下: ...
- CentOS下Apache安装SSL
https是一个安全的访问方式,数据在传输过程中是加密的.https基于ssl. 一.安装apache和ssl模块1.安装apacheyum install httpd2.安装ssl模块yum ins ...
- Oracle【IT实验室】数据库备份与恢复之三:OS备份/用户管理的备份与恢复
用户管理的备份与恢复也称 OS物理备份,是指通过数据库命令设置数据库为备份 状态,然后用操作系统命令,拷贝需要备份或恢复的文件.这种备份与恢复需要用户的 参与手工或自动完成. 对于使用 OS拷贝备份的 ...
- bootstrap组件 2
bootstrap组件2 <!DOCTYPE html> <html lang="zh-cn"> <head > <meta charse ...
- 趣味算法:字符串反转的N种方法(转)
老赵在反对北大青鸟的随笔中提到了数组反转.这的确是一道非常基础的算法题,然而也是一道很不平常的算法题(也许所有的算法深究下去都会很不平常).因为我写着写着,就写出来8种方法……现在我们以字符串的反转为 ...
- document.body / document.ducumentElement /等获取高度和宽度的区别 ----转载
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- 【转】HBase原理和设计
简介 HBase —— Hadoop Database的简称,Google BigTable的另一种开源实现方式,从问世之初,就为了解决用大量廉价的机器高速存取海量数据.实现数据分布式存储提供可靠的方 ...