当前位置:网站首页>Leetcode Brushing Questions - 148. Sort Linked List
Leetcode Brushing Questions - 148. Sort Linked List
2022-08-09 03:05:00 【lonelyMangoo】
题目地址:148. 排序链表
暴力解法
暴力思路很简单,Get all the elements of the linked list,然后排序,Build a new linked list.
public ListNode sortList(ListNode head) {
List<Integer> list = new ArrayList<>();
while (head!=null){
list.add(head.val);
head=head.next;
}
int[] array = list.stream().mapToInt(Integer::intValue).toArray();
Arrays.sort(array);
ListNode listNode = new ListNode();
ListNode res = listNode;
for (int i : array) {
listNode.next = new ListNode(i);
listNode = listNode.next;
}
return res.next;
}
进阶——归并排序
进阶:你可以在 O(n log n) 时间复杂度和常数级空间复杂度下,对链表进行排序吗?
要求 O(n log n)时间复杂度,There are quick sort, heap sort, merge sort, etc,But due to the inconvenience of pointers,There is no doubt that using merge is better here.
There are several caveats to using merge on linked lists
- Setting up a unified head node is easy to operate
- to guarantee constant space,When merging two by two, use the cutting list:Each time according to the current merged step size,Cut into the currently required string,Then return to the back head,This ensures space,Continuity is also guaranteed.
public ListNode sortList(ListNode head) {
ListNode dummyNode = new ListNode(0);
//Unified tail node
dummyNode.next = head;
//每次两个 遍历
int len =0;
while(head!=null){
head=head.next;
len++;
}
ListNode work;
ListNode tail;
for (int step = 1; step < len; step<<=1) {
// 开始操作
//左侧第一个节点
work = dummyNode.next;
//
tail = dummyNode;
while (work!=null){
//tail用来拼接
ListNode left = work;
ListNode right = cut(left,step);
//cut Don't cut it off directly at the back left-step
work = cut(right,step);
//merge
tail.next=merge(left,right);
while (tail.next!=null){
tail=tail.next;
}
}
}
return dummyNode.next;
}
private ListNode cut(ListNode left, int step) {
while (--step!=0 && left!=null ){
left=left.next;
}
//Cut the current one with the next one,and will return later
if(left!=null){
ListNode result = left.next;
left.next = null;
return result;
//If there is no more within the specified step size,直接返回空值
}else{
return null;
}
}
private static ListNode merge(ListNode list1, ListNode list2) {
ListNode res = new ListNode(0);
ListNode sorted = res;
while (list1 !=null && list2!=null){
if(list1.val<list2.val){
res.next=list1;
list1=list1.next;
}else {
res.next=list2;
list2=list2.next;
}
res= res.next;
}
if(list1 == null){
res.next=list2;
}else {
res.next = list1;
}
return sorted.next;
}
边栏推荐
- pytorch 自定义dataset
- 20220528动态规划:最长递增子序列
- 【洛谷】P1082 同余方程
- Shell脚本:函数
- How to deal with cyber attacks?
- Promoting practice with competitions-Like the 84th biweekly game reflection and the 305th weekly game supplementary questions
- LeetCode_43_字符串相乘
- 马斯克被因狗狗币被索赔2580亿美元 操纵价格牟利?狗狗币已下跌92%
- ERROR:Module not found: Error: Can‘t resolve ‘core-js/modules/es.promise.js‘ in ‘address‘
- 以赛促练-力扣第84场双周赛反思以及第305场周赛补题
猜你喜欢
随机推荐
多线程 (进阶+初阶)
一款免费的强大办公工具。
掌握 TypeToken 原理及泛型擦除
Image.new() 及 img.paste() 的用法记录
非关系型数据库MongoDB:(二)副本集部署说明、数据迁移、限制内存、启用mongo认证
作为常用的荧光标记试剂Cy5 亚磷酰胺(CAS号:182873-67-2)有哪些特点了?
lvs+keepalived高可用负载均衡集群
JavsScript系列-Promise的错误捕获
第二部分:和查找表相关的问题
Matlab实现异构交通流
DSPE-PEG-OH,DSPE-PEG-Hydroxyl,磷脂-聚乙二醇-羟基仅供科研实验使用
Hudi从内核到实战介绍
ARM开发(二)ARM体系结构——ARM,数据和指令类型,处理器工作模式,寄存器,状态寄存器,流水线,指令集,汇编小练习题
Talk about those marketing tools - coupons
高并发+海量数据下如何实现系统解耦?【中】
对线面试官实现去重和幂等
Lottie进阶和原理分析
20220523搜索和排序:搜索旋转排序数组
Leetcode刷题——148. 排序链表
20220529设计问题:二叉树的序列化与反序列化