当前位置:网站首页>【LeetCode-147】对链表进行插入排序
【LeetCode-147】对链表进行插入排序
2022-08-11 05:30:00 【Ring*】
6.15 对链表进行插入排序【147】
6.15.1 题目描述
给定单个链表的头 head ,使用 插入排序 对链表进行排序,并返回 排序后链表的头 。
插入排序 算法的步骤:
- 插入排序是迭代的,每次只移动一个元素,直到所有元素可以形成一个有序的输出列表。
- 每次迭代中,插入排序只从输入数据中移除一个待排序的元素,找到它在序列中适当的位置,并将其插入。
- 重复直到所有输入数据插入完为止。
下面是插入排序算法的一个图形示例。部分排序的列表(黑色)最初只包含列表中的第一个元素。每次迭代时,从输入数据中删除一个元素(红色),并就地插入已排序的列表中。
对链表进行插入排序。
6.5.2 方法一:从前往后找插入点
class Solution {
public ListNode insertionSortList(ListNode head) {
if (head == null) {
return head;
}
ListNode dummyHead = new ListNode(0);
dummyHead.next = head;
ListNode lastSorted = head, curr = head.next;
while (curr != null) {
if (lastSorted.val <= curr.val) {
lastSorted = lastSorted.next;
} else {
ListNode prev = dummyHead;
while (prev.next.val <= curr.val) {
prev = prev.next;
}
lastSorted.next = curr.next;
curr.next = prev.next;
prev.next = curr;
}
curr = lastSorted.next;
}
return dummyHead.next;
}
}
复杂度分析
- 时间复杂度: O ( n 2 ) O(n^2) O(n2),其中 n 是链表的长度。
- 空间复杂度:O(1)。
6.5.3 my answer—插入排序
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */
class Solution {
public static ListNode insertionSortList(ListNode head) {
if(head==null)return null;
ListNode dummy = new ListNode(0,head);
ListNode cur = head;
ListNode a = null;
while(cur != null && cur.next != null){
// 判断当前结点cur的下一个结点,需不需要插入到前面某结点间
boolean flag = false; // 用于判断是否
a = dummy;
while (a.next != cur.next){
if(a.next.val> cur.next.val){
// 满足条件,需要插入到a.next位置
ListNode temp1 = a.next;
ListNode temp2 = cur.next.next;
a.next = cur.next;
cur.next.next = temp1;
cur.next = temp2;
flag = true;
break;
}
a = a.next;
}
// 当前结点cur.next未进行插入到前面的操作,才需要cur后移。插入了就不需要后移,因为有cur.next.next补上来
if(!flag){
cur = cur.next;
}
}
return dummy.next;
}
}
边栏推荐
- heap2 (tcache attack,house of orange)
- Use the adb command to manage applications
- Thesis unscramble TransFG: A Transformer Architecture for Fine - grained Recognition
- 字节(byte)和位(bit)
- The mount command - mounted read-only, solution
- Day 69
- c语言-数据存储部分
- OpenMLDB v0.5.0 released | Performance, cost, flexibility reach new heights
- Scene-driven feature calculation method OpenMLDB, efficient implementation of "calculate first use"
- Manufacturer Push Platform-Huawei Access
猜你喜欢
JS事件循环机制
OpenMLDB: Consistent production-level feature computing platform online and offline
微信小程序启动页的实现
OpenMLDB:线上线下一致的生产级特征计算平台
Jetpack使用异常问题集锦
C语言-7月21日-指针的深入
He Kaiming's new work ViTDET: target detection field, subverting the concept of layered backbone
heap2 (tcache attack,house of orange)
swagger错误:WARN i.s.m.p.AbstractSerializableParameter - [getExample,421] - Illegal DefaultValue null
C语言-内存操作函数
随机推荐
微信小程序云开发项目wx-store代码详解
品优购项目实战笔记
活动预告 | 4月23日,多场OpenMLDB精彩分享来袭,不负周末好时光
OpenMLDB Meetup No.2 会议纪要
轻松理解进程与线程
Day 72
swagger常用注释API @ApiModel、@ApiModelProperty的用法
Day 86
使用adb命令管理应用
Byte (byte) and bit (bit)
2021-09-11 C language variables and memory allocation
Day 82
Visual studio2019 配置使用pthread
OpenMLDB Pulsar Connector:高效打通实时数据到特征工程
heap2 (tcache attack,house of orange)
Here is a memorial
Real-time Feature Computing Platform Architecture Methodology and Practice Based on OpenMLDB
Day 71
Getting Started with JNI
JS案例练习(pink老师经典案例)