Sort a linked list in O(n log n) time using constant space complexity.
来源:互联网 发布:儿童安踏篮球鞋淘宝 编辑:程序博客网 时间:2024/05/17 23:14
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
static bool sort_by_value(const int &a,const int &b)
{
return a<b;
}
ListNode *sortList(ListNode *head)
{
vector<int> res;
if(head == NULL)
return head;
ListNode *temp = head;
while(temp != NULL)
{
res.push_back(temp->val);
temp = temp->next;
}
sort(res.begin(),res.end());
//sort(res.begin(),res.end().sort_by_value);
temp = head;
vector<int>::iterator iter = res.begin();
while(temp != NULL)
{
temp->val = *iter;
iter++;
temp = temp->next;
}
return head;
}
};
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
static bool sort_by_value(const int &a,const int &b)
{
return a<b;
}
ListNode *sortList(ListNode *head)
{
vector<int> res;
if(head == NULL)
return head;
ListNode *temp = head;
while(temp != NULL)
{
res.push_back(temp->val);
temp = temp->next;
}
sort(res.begin(),res.end());
//sort(res.begin(),res.end().sort_by_value);
temp = head;
vector<int>::iterator iter = res.begin();
while(temp != NULL)
{
temp->val = *iter;
iter++;
temp = temp->next;
}
return head;
}
};
1 0
- Sort a linked list in O(n log n) time using constant space complexity
- Sort a linked list in O(n log n) time using constant space complexity.
- Sort a linked list in O(n log n) time using constant space complexity.
- Sort a linked list in O(n log n) time using constant space complexity.
- Sort a linked list in O(n log n) time using constant space complexity.
- Sort a linked list in O(n log n) time using constant space complexity.
- leetcode Sort a linked list in O(n log n) time using constant space complexity.
- leetcode Sort List (Sort a linked list in O(n log n) time using constant space complexity)
- leetcode_Sort a linked list in O(n log n) time using constant space complexity.
- separate odd and even numbers in an array in O(n) time complexity and O(1) space complexity
- LeetCode234——Palindrome Linked List,O(n) time and O(1) space
- Solution to Maximum Subarray in linear-time algorithm with time complexity is O(n)
- Reverse a doubly linked list in O(1) time
- Sort a linked list using insertion sort
- Sort a linked list using insertion sort
- Find duplicates in O(n) time and O(1) extra space
- leetcode_Sort a linked list using insertion sort.
- 算法的基础知识( Time Complexity & Space Complexity& Big O notation)
- 第一篇blog
- Maven-Eclipse搭建一个maven项目(方法二)
- 贝叶斯定理
- Java 注解 详细介绍
- Gym 101257 (2017 Hackatari Codeathon)
- Sort a linked list in O(n log n) time using constant space complexity.
- Roguelike游戏设计(一)
- LINUX系统中安装Mysql5.5
- 1
- 删除目录中的文件
- (转)Android中XML tools属性详解
- jFinal中setCookie和getCookie的使用,并解决中文乱码
- vue中style与class的绑定
- 关于习惯的养成