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的更多相关文章

  1. leetcode: sortlist之四种方法

    原题链接:https://oj.leetcode.com/problems/sort-list/ 题目:空间复杂度为常数,时间复杂度为O(nlogn)的排序链表实现 方法一:第一想法是模拟数组的快速排 ...

  2. sort-list leetcode C++

    Sort a linked list in O(n log n) time using constant space complexity. C++ /** * Definition for sing ...

  3. [LeetCode] Sort List 链表排序

    Sort a linked list in O(n log n) time using constant space complexity. 常见排序方法有很多,插入排序,选择排序,堆排序,快速排序, ...

  4. leetcode笔记

    82. Remove Duplicates from Sorted List II https://leetcode.com/problems/remove-duplicates-from-sorte ...

  5. leetcode算法分类

    利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...

  6. leetcode bugfree note

    463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...

  7. LeetCode题目分类

    利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...

  8. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

  9. 【Leetcode】Sort List JAVA实现

    Sort a linked list in O(n log n) time using constant space complexity. 1.分析 该题主要考查了链接上的合并排序算法. 2.正确代 ...

随机推荐

  1. 如何设计一个 iOS 控件?(iOS 控件完全解析) (转)

    前言 一个控件从外在特征来说,主要是封装这几点: 交互方式 显示样式 数据使用 对外在特征的封装,能让我们在多种环境下达到 PM 对产品的要求,并且提到代码复用率,使维护工作保持在一个相对较小的范围内 ...

  2. CLR via C#学习笔记---类型

    类的内存分配:http://www.cnblogs.com/JimmyZhang/archive/2008/01/31/1059383.html 关键字: abstract     (类)该类不能构建 ...

  3. Ubuntu开机自启动

    http://www.jb51.net/os/Ubuntu/181138.html http://blog.csdn.net/elim051/article/details/6173367

  4. 【翻译十七】java-并发之高性能对象

    High Level Concurrency Objects So far, this lesson has focused on the low-level APIs that have been ...

  5. 解决oracle11g 空表不能exp导出的问题

    在使用exp备份数据库,然后使用imp导入的时候出现了好多表或者视图不存在的错误信息. 究其原因,是11G中增加了一个新的特性:数据条数是0时不分配segment,所以就不能被导出. 解决思路:就是向 ...

  6. Python科学计算发行版—Anaconda

    Python是一种强大的编程语言,其提供了很多用于科学计算的模块,常见的包括numpy.scipy和matplotlib.要利用Python进行科学计算,就需要一一安装所需的模块,而这些模块可能又依赖 ...

  7. html5 svg 圆形进度条

    html5 svg 圆形进度条 <!DOCTYPE html> <html lang="en"> <head> <meta charset ...

  8. js判断手机端Android手机还是iPhone手机

    /*判断当前设备是平板.安卓.苹果设备*/ <script type="text/javascript"> function fBrowserRedirect(){ v ...

  9. 将数据导出成excel表

    /// <summary> /// 生成excel表 /// </summary> /// <param name="dt">数据表</p ...

  10. hdu 4291 2012成都赛区网络赛 矩阵快速幂 ***

    分析:假设g(g(g(n)))=g(x),x可能非常大,但是由于mod 10^9+7,所以可以求出x的循环节 求出x的循环节后,假设g(g(g(n)))=g(x)=g(g(y)),即x=g(y),y也 ...