Leetcode # 148 Sort List
2015-07-31 22:24
267 查看
Sort a linked list in O(n log n)
time using constant space complexity.
Difficulty:Medium
借鉴之前的merge two sorted list,类似qsort的方法排序.
time using constant space complexity.
Difficulty:Medium
借鉴之前的merge two sorted list,类似qsort的方法排序.
</pre><pre name="code" class="cpp">ListNode* getMid(ListNode* curr) { ListNode* p = curr; ListNode* p1 = curr; while(p1->next!=NULL&&p1->next->next!=NULL) { p = p->next; p1 = p1->next->next; } return p; } ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { if(l1==NULL) return l2; if(l2==NULL) return l1; ListNode* p=NULL; if(l1->val>=l2->val){ p = l2; p->next = mergeTwoLists(l2->next,l1); } if(l2->val>l1->val){ p = l1; p->next = mergeTwoLists(l1->next,l2); } return p; } ListNode* sortList(ListNode* head) { if(head==NULL||head->next==NULL) return head; ListNode* mid = getMid(head); ListNode* next = mid->next; mid->next = NULL; return mergeTwoLists(sortList(head),sortList(next)); }
相关文章推荐
- 人生最大的捷径,是用时间和生命读一流的书
- 计蒜客ACM 最大子阵列 java
- Struts2中Action返回Json串
- ( ̄▽ ̄") 没钱了
- p2098
- POJ 1797 Heavy Transportation
- MVC——studying
- Binary Tree Maximum Path Sum
- PHP-FPM高负载的解决办法
- 事务的隔离级别解析
- Linux 安装rabbitmq(centos 6.5) 步骤
- iOS学习(UIImageView)
- 。◕‿◕。TMD
- 找单链表的中间位置
- python 对EXCEL的读写操作
- ubuntu下vagrant up经常出现Failed to upload a file to the guest VM via SCP due to a permissions error.最终解决
- 把握linux内核设计思想(九):进程调度
- android的Spinner控件的自定义样式设置以及ArrayAdapter的使用
- Windows命令行停止、启用、禁用服务
- div+css position定位浅析