LintCode 链表排序
2017-03-31 10:01
435 查看
题目描述:
在 O(n log n) 时间复杂度和常数级的空间复杂度下给链表排序。
样例
给出 1->3->2->null,给它排序变成 1->2->3->null.
思路分析:
sort扫一遍~
ac代码:
在 O(n log n) 时间复杂度和常数级的空间复杂度下给链表排序。
样例
给出 1->3->2->null,给它排序变成 1->2->3->null.
思路分析:
sort扫一遍~
ac代码:
/** * Definition of ListNode * class ListNode { * public: * int val; * ListNode *next; * ListNode(int val) { * this->val = val; * this->next = NULL; * } * } */ class Solution { public: /** * @param head: The first node of linked list. * @return: You should return the head of the sorted linked list, using constant space complexity. */ ListNode *sortList(ListNode *head) { // write your code here int a[1000005]; int top=0; while(head!=NULL) { a[top++]=head->val; head=head->next; } sort(a,a+top); ListNode *dummy,*r,*s; dummy=new ListNode(0); r=dummy; for(int i=0;i<top;i++) { s=new ListNode(a[i]); r->next=s; r=s; } return dummy->next; } };
相关文章推荐
- LintCode—删除排序链表中的重复元素(112)
- LintCode—合并两个排序链表(165)
- Lintcode112.删除排序链表中的重复元素
- Google/LintCode:M-合并k个排序链表
- lintcode 165. 合并两个排序链表
- lintcode-173-链表插入排序
- lintcode——删除排序链表中的重复元素
- lintcode merge-k-sorted-lists 合并k个排序链表
- LintCode-剑指Offer-(165)合并两个排序链表
- LintCode--合并k个排序链表
- LintCode-剑指Offer-(112)删除排序链表中的重复元素
- LintCode-删除排序链表中的重复元素
- lintcode 165 合并两个排序链表
- lintcode——合并两个排序链表
- lintcode,链表排序
- LintCode 链表插入排序
- LintCode刷题链表排序——归并和快速排序
- LintCode_112_删除排序链表中重复的元素
- lintcode :链表插入排序
- lintcode合并两个排序链表