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. Loadrunner中Throughput(吞吐量)的分析与计算

    Throughput翻译为吞吐量,按照常规理解网络吞吐量表示在单位时间内通过网卡数据量之和,其中即包括本机网卡发送出去的数据量也包括本机网卡接收到的数据量,但这个理解在Loadrunner记录的Thr ...

  2. SQL跨项目查询语法

    EXEC sp_addlinkedserver 'ITSV', '', 'SQLOLEDB', '192.168.1.248' EXEC sp_addlinkedsrvlogin 'ITSV', 'f ...

  3. C#的扩展方法

    using System; using System.Collections; using System.Collections.Generic; using System.IO; using Sys ...

  4. 16.迭代器模式(Iterator Pattern)

    using System; namespace ConsoleApplication9 { class Program { /// <summary> /// 迭代器模式提供了一种方法顺序 ...

  5. JavaWeb学习之JSTL自定义标签库的使用、JSTL自定义函数库(7)

    一.自定义标签,步骤 * 确定需求 * <my:date /> 输出当前系统的时间 yyyy-MM-dd hh:mm:ss:SSS * 编写Java类 新建包名:com.yxl.tag,新 ...

  6. ASP.NET Web API 配置返回的json字段的格式以及Action返回HttpResponseMessage类型和IHttpActionResult类型

    1. 对于返回的Json对象格式是以“帕斯卡”风格的(例如“FirstName”),然而我们的Api有很大的可能被带有Javascript的客户端消费,对于JS开发者来说可能更适合“驼峰”风格(例如” ...

  7. Linux环境下段错误的产生原因及调试方法小结

    转载自http://www.cnblogs.com/panfeng412/archive/2011/11/06/2237857.html 最近在Linux环境下做C语言项目,由于是在一个原有项目基础之 ...

  8. 【翻译二十】-java线程池

    Thread Pools Most of the executor implementations in java.util.concurrent use thread pools, which co ...

  9. java中常用的工具类(二)

    下面继续分享java中常用的一些工具类,希望给大家带来帮助! 1.FtpUtil           Java   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...

  10. <转>ORA-12154: TNS: 无法解析指定的连接标识符

    相信作为ORACLE数据库的开发人员没有少碰到“ORA-12154: TNS: 无法解析指定的连接标识符”,今天我也又碰到了类似的情况,将我的解决方法进行小结,希望能对碰到同样问题的友人们提供帮助. ...