LeetCode OJ:Sort List(排序链表)
Sort a linked list in O(n log n) time using constant space complexity.
题目要求在常数控件内以O(nlogn)的事件复杂度来排序链表。
常数空间内我没有实现,O(nlogn)的话使用归并排序就可以了吗, 下面是代码:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* sortList(ListNode* head) {
if(!head || !head->next)
return head;
return mergeSort(head);
} ListNode * mergeSort(ListNode * head)
{
if(head == NULL || head->next == NULL) return head;
ListNode * fastP = head;
ListNode * slowP = head;
ListNode * slowPre = slowP; //这里生成快慢指针的时候应该注意一点,选一个pre节点,否则直接用slowP的话那么分成的两段是不平衡的
for(; fastP != NULL && fastP->next != NULL; fastP = fastP->next->next, slowP = slowP->next){
slowPre = slowP;
}
ListNode * head1 = head;
ListNode * head2 = slowP;
slowPre->next = NULL;//截断list
head1 = mergeSort(head1);
head2 = mergeSort(head2);
return merge(head1, head2);
} ListNode * merge(ListNode * head1, ListNode * head2)
{
ListNode * ret = new ListNode(); //记录首节点的位置
ListNode * helper = ret; //这里应该注意,helper是用来标记下一个插入位置用的。
while(head1 && head2){
if(head1->val < head2->val){
helper->next = head1;
head1 = head1->next;
}else{
helper->next = head2;
head2 = head2->next;
}
helper = helper->next;//指向当前链表的尾节点
}
if(head1 == NULL)
helper->next = head2;
else
helper->next = head1;
helper = ret->next;
ret->next = NULL; //销毁helper节点
delete ret;
return helper;
} };
下面的事java版,写了很多的局部变量,主要事图个方便啊,方法与上相同,可以满足题目的限制条件。代码如下所示:
public class Solution {
public ListNode sortList(ListNode head) {
if(head == null || head.next == null)
return head;
return mergeSort(head);
}
public ListNode mergeSort(ListNode head){
if(head == null || head.next == null) return head;
ListNode fast = head;
ListNode slow = head;
ListNode slowPre = new ListNode(-1);
slowPre.next = head;
while(fast != null){
fast = fast.next;
slow = slow.next;
slowPre = slowPre.next;
if(fast != null)
fast = fast.next;
}
ListNode list1 = head;
ListNode list2 = slow;
slowPre.next = null;
list1 = mergeSort(list1);
list2 = mergeSort(list2);
return merge(list1, list2);
}
public ListNode merge(ListNode head1, ListNode head2){
if(head1 == null) return head2;
if(head2 == null) return head1;
ListNode helperP = new ListNode(-1);
helperP.next = head1;
ListNode pPre = helperP;
ListNode p = head1;
ListNode helperQ = new ListNode(-1);
helperQ.next = head2;
ListNode qPre = helperQ;
ListNode q = head2;
while(p != null && q != null){
if(p.val < q.val){
p=p.next;
pPre = pPre.next;
}else{
pPre.next = q;
qPre.next = q.next;
q.next = p;
q = qPre.next;
pPre = pPre.next;
}
}
if(p == null){
pPre.next = helperQ.next;
helperQ.next = null;
}
return helperP.next;
}
}
LeetCode OJ:Sort List(排序链表)的更多相关文章
- LeetCode - 82、删除排序链表中的重复元素 II
给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字. 示例 1: 输入: 1->2->3->3->4->4->5 输出: 1 ...
- LeetCode - 83、删除排序链表中的重复元素
给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 示例 1: 输入: 1->1->2 输出: 1->2 示例 2: 输入: 1->1->2->3 ...
- [LeetCode] Wiggle Sort 摆动排序
Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] < ...
- [LeetCode 109] - 将已排序链表转换为二叉搜索树 (Convert Sorted List to Binary Search Tree)
问题 给出一个元素以递增序列排序的单链表,将其转换为一棵高度平衡的二叉搜索树. 初始思路 二叉搜索树高度平衡,意味着左右子树的高度要平衡.根据二叉树左子树节点小于根节点,右子树节点大于根节点的性质:我 ...
- [Leetcode] 第148题 排序链表
一.题目描述 在 O(n log n) 时间复杂度和常数级空间复杂度下,对链表进行排序. 示例 1: 输入: 4->2->1->3 输出: 1->2->3->4 示 ...
- Leetcode148. Sort List排序链表
在 O(n log n) 时间复杂度和常数级空间复杂度下,对链表进行排序. 示例 1: 输入: 4->2->1->3 输出: 1->2->3->4 示例 2: 输入 ...
- [LeetCode OJ] Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- leetcode——Insertion Sort List 对链表进行插入排序(AC)
Sort a linked list using insertion sort. class Solution { public: ListNode *insertionSortList(ListNo ...
- [LeetCode题解]83. 删除排序链表中的重复元素 | 递归 + 迭代
方法一:递归 解题思路 通过递归法,每次判断目前头节点与给定的节点是否相等.如是,继续判断下一个节点,否则保存当前头节点,设置 next 指向下次递归得到的节点,然后返回当前节点. 代码 /** * ...
- LeetCode【83. 删除排序链表中的重复元素】
我最开始的程序是 但是结果
随机推荐
- (转)js原生自定义事件的触发dispatchEvent
1. 对于标准浏览器,其提供了可供元素触发的方法:element.dispatchEvent(). 不过,在使用该方法之前,我们还需要做其他两件事,及创建和初始化.因此,总结说来就是: 1 2 3 d ...
- from PyQt4.QtGui import * 提示 ImportError: DLL load failed: %1 is not a valid Win32 application.
个人用64位电脑安装了64位的PyQt后 from PyQt4.QtGui import * 提示 ImportError: DLL load failed: %1 is not a valid Wi ...
- 剑指offer 面试32题
面试32题: 题目:从上到下打印二叉树 题:不分行从上到下打印二叉树 解题代码: # -*- coding:utf-8 -*- # class TreeNode: # def __init__(sel ...
- 剑指offer 面试55题
面试55题: 题目:二叉树的深度 题:输入一棵二叉树,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度. 解题思路: ①如果一棵树只有一个节点,它 ...
- iOS Swift 熊猫🐼跑酷 第一个小项目
前言:想用swift 写个小游戏 慢慢转化 能写出 ARKit来.但是又不能一口吃个胖子,慢慢来,在网络视频教程中撸了视频教学,断断续续看了半个多月,基本实现了 游戏主角
- 大道至简(第五i章)读后感
大道至简(第五章)读后感 再一次在不想看的情况下读大道至简第五章,一个项目的实现中,“过程”与“工程”是同一个概念吗?答案自然是否定的.“过程”是一个确定的模板,而“工程”是有一个目的的实现在里面. ...
- four application:geocoder widget
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- imx6solo wm8960始终没有声音输出
我尝试各种办法,wm8960始终不能得到声音输出.调试过程如下: 首先,打开电源使能脚: ret=gpio_request(SABRESD_CODEC_PWR_EN,"audio_pwr_e ...
- webstrom突然不显示文件夹
不知道什么原因,webstrom突然不显示文件夹了,弄得几乎都不能用了... 百度搜了一下解决方案: 问题原因:webstorm自动生成的配置文件 .idea/modules.xml损坏(就是我一开始 ...
- mysql性能优化的一些建议
mysql性能优化的一些建议 1.EXPLAIN 你的 SELECT 查询 查看rows列可以让我们找到潜在的性能问题. 2.为关键字段添加索引,比如:where, order by, group b ...