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.正确代 ...
随机推荐
- gitlab 建仓的流程
repository:仓库 Git global setup: git config --global user.name "Administrator" git config - ...
- XAML语言介绍
<Window x:Class="WpfApplication1.Window1" xmlns="http://schemas.microsoft.com/winf ...
- 在MAVEN仓库中添加ORACLE JDBC驱动
本文转载自 http://www.cnblogs.com/leiOOlei/archive/2013/10/21/3380568.html 因为已经是第二次遇到,所以COPY过来,怕以后别人的BLOG ...
- 针对较大基数的排列组合算法Java实现类(n选m)
package com.utils; import java.math.BigDecimal; import java.math.RoundingMode; public class PLZUUtil ...
- 在Activity和Application中使用SharedPreferences存储数据
1.在Activity中创建SharedPreferences对象及操作方法 SharedPreferences pre=getSharedPreferences("User", ...
- CQRS及.NET中的参考资料
(此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:CQRS作为一种设计模式,其实一点都不新鲜了.不过今天有朋友感叹.NET朋友也关注CQ ...
- Zero Copy 简介
转自:http://blog.csdn.net/zzz_781111/article/details/7534649 许多web应用都会向用户提供大量的静态内容,这意味着有很多data从硬盘读出之后, ...
- 字节流、字符串、16进制字符串转换__Java(转)
/** * @Package: * @ClassName:TypeConversion * @Description:字节流.字符串.16进制字符串转换 * @author:xk * @date:Ja ...
- 智能车学习(十八)——电机学习
一.C车电机选择 1.摘要: 因为C车模在四轮车的优势是有两个电机,可以进行主动差速,劣势是电机太弱了....所以如何选择电机,就是个钱的问题了,电机多一点,就比较好选,但是C车电机跑多了就 ...
- hdu 并查集分类(待续)
hdu 1829 A Bug's Life 题目大意: 给你n个动物,输入m行a,b,表示a和b应该是异性的,要你判断是否有同性恋. 并查集中,1到n代表应性别,n+1到2n代表一个性别,合并一下,判 ...