当前位置:网站首页>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;
}

边栏推荐
猜你喜欢

【信号去噪】基于Sage-Husa自适应卡尔曼滤波器实现海浪磁场噪声抑制及海浪磁场噪声的产生附matlab代码

dice和iou

Redis中SDS简单动态字符串

【机器学习】21天挑战赛学习笔记(三)

phpStdudy的下载和DVWA的搭建

SQL注入(2)

ARM开发(二)ARM体系结构——ARM,数据和指令类型,处理器工作模式,寄存器,状态寄存器,流水线,指令集,汇编小练习题

作为常用的荧光标记试剂Cy5 亚磷酰胺(CAS号:182873-67-2)有哪些特点了?

Second data CEO CAI data warming invited to jointly organize the acceleration data elements online salon

下秒数据CEO蔡致暖受邀参加联合数据举办《数据要素加速跑》线上沙龙
随机推荐
机器学习入门
全文翻译:Multimodal Neural Networks: RGB-D for Segmantic Segmentation and Object Detection
智能计数器控制板的功能及应用有哪些?
leetcode-23.合并K个升序链表
Second data CEO CAI data warming invited to jointly organize the acceleration data elements online salon
20220526动态规划:不同路径
powershell execution strategy
JSON beautification plugin for Chrome
C18-PEG- ALD批发_C18-PEG-CHO_C18-PEG-醛基
【洛谷】P5091 【模板】扩展欧拉定理
iFLYTEK Written Exam Questions Review
C语言力扣第56题之合并区间。排序+双指针
【扫雷--2】
C专家编程 第10章 再论指针 10.2 指针数组就是Iliffle向量
【洛谷】P1082 同余方程
浅聊一下那些营销工具—优惠券
Kubernetes:(十五)PV与PVC的《恩怨情仇》
C专家编程 第9章 再论数组 9.6 C语言的多维数组
Solve the Final Fantasy 13-2 Clock Puzzle with DFS
7月更新速递 | 产品实验室N+1,EasyV For Unreal上线!