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.正确代 ...
随机推荐
- Java -- File
@.getPath().getAbsolutePath().getCanonicalPath()区别 原文:http://blog.csdn.net/wh_19910525/article/detai ...
- HTML5学习之跨文档传输消息(七)
新标准中提供了文档之间直接的消息传输API.而且不限制跨域消息传递! 发送消息使用的是Window对象的postMessage(data,targetURL)方法就可以了,但给哪个window对象发送 ...
- 7z命令行工具
7z (中文)是优秀开源的压缩解压缩软件(wiki: en 中文),有windows版本与linux版本,最新的9.32版本支持的格式包括: 压缩与解压缩均支持:7z, XZ, BZIP2, GZI ...
- POJ3685 Matrix(嵌套二分)
同行元素递减,同列元素递增,采用嵌套二分的方法 #include<cstdio> #include<iostream> #include<cstdlib> #inc ...
- scala中的trait
这里的trait字面意思是特质或者特征,这个词翻译成特征比较合适.它的意义和java,c#中接口很类似.但是trait支持部分实现,也就是说可以在scala的trait中可以实现部分方法. 下面我们以 ...
- phpcms v9 wap内容页内容显示方法
phpcms v9的wap手机门户的问题解决 默认的{$content}标签假如内容页一开始输入的不是html代码的话会出现调用不出来的情况,这里用{$rs['content']} 来调用则可以解决问 ...
- php 以图搜图
感知哈希算法count < =5 匹配最相似count > 10 两张不同的图片var_dump(ImageHash::run('1.jpg’, '2.jpg’)); <?php c ...
- AgileEAS.NET SOA 中间件2013第四季度发布&部分功能开源预告
一.前言 AgileEAS.NET SOA 中间件平台是一款基于基于敏捷并行开发思想和Microsoft .Net构件(组件)开发技术而构建的一个快速开发应用平台.用于帮助中小型软件企业建立一条适合市 ...
- WPF QuickStart系列之线程模型(Thread Model)
这篇博客将介绍WPF中的线程模型. 首先我们先来看一个例子,用来计算一定范围内的素数个数. XAML: <Grid> <Grid.RowDefinitions> <Row ...
- 回忆一次面试Android研发的问题
有NDK开发JNI程序经验优先 intent intentfileter 阿里云 线程,异步 1.图片缓冲2.解压3.获取搜索记录 4.在安卓开发过程中用到那些框架