LeetCode.148 Sort List
来源:互联网 发布:四大名著 知乎 编辑:程序博客网 时间:2024/06/08 07:06
题目:
Sort a linked list in O(n log n) time using constant space complexity.
分析:
时间复杂度nlogn,首先想到的是归并排序或快速排序
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */class Solution { public ListNode sortList(ListNode head) { //利用经典的三段式归并排序,小,相等,大 if(head==null||head.next==null) return head; ListNode smaller=new ListNode(0); ListNode bigger=new ListNode(0); //pres代表小,pre代表相等,preb代表大 ListNode pres=smaller; ListNode pre=head; ListNode preb=bigger; //当前操作的节点 ListNode cur=head.next; while(cur!=null){ //和当前head节点比较 if(cur.val<head.val){ pres.next=cur; pres=pres.next; }else if(cur.val>head.val){ preb.next=cur; preb=preb.next; }else{ //相等 pre.next=cur; pre=pre.next; } cur=cur.next; } //分段结束后,将各段链表末尾置空,为后面拼接准备 pres.next=pre.next=preb.next=null; //递归划分小的部分和大部分 smaller.next=sortList(smaller.next); bigger.next=sortList(bigger.next); //将三段链表拼接 //因为pres为smaller //需要将pres置为最后 while(pres.next!=null){ pres=pres.next; } pres.next=head; //bigger.next因为是bigger带头节点,所以需要取next pre.next=bigger.next; return smaller.next; }}归并排序:
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */class Solution { public ListNode sortList(ListNode head) { //使用归并排序 //判断是否为空 if(head==null||head.next==null)return head; //划分两部分,使用快慢指针 ListNode fast=head,slow=head,pre=null; while(fast!=null&&fast.next!=null){ pre=slow; slow=slow.next; fast=fast.next.next; } //分开 pre.next=null; //递归划分两部分 ListNode h1=sortList(head); ListNode h2=sortList(slow); //合并 return merge(h1,h2); } //合并 public ListNode merge(ListNode l1,ListNode l2){ ListNode dummy=new ListNode(0); ListNode cur=dummy; //典型的合并思想 while(l1!=null&&l2!=null){ if(l1.val<l2.val){ cur.next=l1; l1=l1.next; }else{ cur.next=l2; l2=l2.next; } cur=cur.next; } //防止还有元素 if(l1!=null)cur.next=l1; if(l2!=null)cur.next=l2; return dummy.next; }}
阅读全文
0 0
- LeetCode(148)Sort List
- LeetCode: Sort List [148]
- LeetCode 148 Sort List
- [leetcode 148] Sort List
- [LeetCode 148]Sort List
- leetcode-148 Sort List
- Leetcode 148 Sort List
- LeetCode 148: Sort List
- 【leetcode 148 Sort List】
- Leetcode[148]-Sort List
- Sort List - LeetCode 148
- Leetcode# 148 Sort List
- Leetcode # 148 Sort List
- LeetCode(148) Sort List
- leetcode 148: Sort List
- [Leetcode]#148 Sort List
- 148 Sort List [Leetcode]
- LeetCode 148 Sort List
- History go() 方法
- 训练集---训练赛12
- SURF-金字塔搭建(忧郁版)
- 第三周项目1
- 前端沉淀-每周阅读文章
- LeetCode.148 Sort List
- 高级软件工程C语言编程实践lab2
- android验证验证码界面ui实现倒计时实现
- html画特殊图形(待修改)
- python中的短路计算
- JAVA正则表达式匹配多个空格
- 启动hadoop时,namenode无法启动,log中出现:java.io.IOException: NameNode is not formatted
- 创建向量,求标准差
- c语言学习 第二次