当前位置:网站首页>对链表进行插入排序[dummy统一操作+断链核心--被动节点]
对链表进行插入排序[dummy统一操作+断链核心--被动节点]
2022-06-25 06:39:00 【REN_林森】
前言
对于链表的操作,统一空链表操作的头节点是非常nice,不仅统计操作,还可随时拿到头节点。
除此之外,单向链表操作核心问题之一就是断链问题,而断链存在的本质就是因为单向所带来后续的被动节点,所以要么先把后面的链接到其他指针后,要么直接用指针指着。
一、对链表进行插入排序
二、dummyHead + 断链保护
package everyday.medium;
// 链表的插入排序。
public class InsertionSortList {
/* target:讲链表进行插入排序,遍历链表,把每个节点插到新链表即可。 注:对于链表,用dummyHead统一空链表操作,非常方便。 */
public ListNode insertionSortList(ListNode head) {
// 特殊情况,特殊处理。
if (null == head) return null;
// 插入排序。
ListNode dummyHead = new ListNode(0);
while (head != null) {
ListNode p = dummyHead;
// 寻找该插入的位置。
while (p.next != null && p.next.val < head.val) p = p.next;
// 记录剩下未排序的部分,防止断链。。
ListNode next = head.next;
head.next = p.next;
p.next = head;
// 继续循环拿剩余节点排序。
head = next;
}
return dummyHead.next;
}
// 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;
}
}
}
总结
1)头节点dummyHead对链表真的非常nice
2)单向链表断链核心,就是需要指针指着的被动状态。
参考文献
边栏推荐
- Genuine photoshop2022 purchase experience sharing
- LeetCode 每日一题——515. 在每个树行中找最大值
- 高考志愿填报,为啥专业最后考虑?
- [C language] one dimensional array
- Harmony food menu interface
- MySQL facet 01
- How to store the directory / hierarchy / tree structure in the database- How to store directory / hierarchy / tree structure in the database?
- 【批处理DOS-CMD命令-汇总和小结】-应用程序启动和调用、服务和进程操作命令(start、call、)
- Alphassl wildcard certificate for one month
- 【UVM入門 ===> Episode_9 】~ 寄存器模型、寄存器模型的集成、寄存器模型的常規方法、寄存器模型的應用場景
猜你喜欢
Sqlmap advanced use – cookies
[C language] one dimensional array
14 bs对象.节点名称.name attrs string 获取节点名称 属性 内容
Make enough money to go back home
栅格地图(occupancy grid map)构建
数据可视化没有重点怎么办?
网络是怎样连接的?
[batch dos-cmd command - summary and summary] - CMD extended command and function (CMD /e:on, CMD /e:off)
Weimeisi new energy rushes to the scientific innovation board: the annual revenue is 1.7 billion, and the book value of accounts receivable is nearly 400million
高数基础_函数的奇偶性
随机推荐
栅格地图(occupancy grid map)构建
Chang Wei (variables and constants) is easy to understand
College entrance examination voluntary filling, why is the major the last consideration?
13 `bs_ duixiang. Tag tag ` get a tag object
我与CSDN的一年时光及大学经验分享
Operate cnblogs metaweblog API
Too beautiful promise because too young
【批处理DOS-CMD命令-汇总和小结】-cmd扩展命令、扩展功能(cmd /e:on、cmd /e:off)
Sqlmap advanced use – cookies
Cocos学习日记3——api获取节点、组件
Editing the date formatting of x-axis tick labels in Matplotlib - editing the date formatting of x-axis tick labels in Matplotlib
用动图讲解分布式 Raft
关于硬件问题造成的MCU死机,过来人简单的谈一谈
Design a MySQL table for message queue to store message data
Vscode official configuration synchronization scheme
Make enough money to go back home
Explain distributed raft with dynamic diagram
Advanced mathematics foundation_ Parity of functions
[XXL job] the pond is green and the wind is warm. I remember that Yu Zhen first met
How to get the difference between two dates rounded to hours