当前位置:网站首页>对链表进行插入排序[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)单向链表断链核心,就是需要指针指着的被动状态。
参考文献
边栏推荐
- Editing the date formatting of x-axis tick labels in Matplotlib - editing the date formatting of x-axis tick labels in Matplotlib
- Redirect to previous page after login? PHP - Redirecting to previous page after login? PHP
- 【批处理DOS-CMD命令-汇总和小结】-cmd扩展命令、扩展功能(cmd /e:on、cmd /e:off)
- VectorDraw Developer Framework 10.10
- Classic paper in the field of character recognition: aster
- Global variables & local variables
- 14 bs对象.节点名称.name attrs string 获取节点名称 属性 内容
- 48 pictures | teach you the performance monitoring, pressure testing and tuning of microservices by hand
- Chang Wei (variables and constants) is easy to understand
- My debut is finished!
猜你喜欢
![[batch dos-cmd command - summary and summary] - add comment command (REM or::)](/img/e9/151885ecd490b0aa83cce0f3a49124.png)
[batch dos-cmd command - summary and summary] - add comment command (REM or::)

网络是怎样连接的?

Intel announced five new technological developments, including quantum computing, neural pseudo computing, machine programming, integrated optoelectronics, and secure computing

正版photoshop2022购买体验经历分享

【批处理DOS-CMD命令-汇总和小结】-cmd扩展命令、扩展功能(cmd /e:on、cmd /e:off)

图扑软件数字孪生 3D 风电场,智慧风电之海上风电

One year's time and University experience sharing with CSDN
![[Yu Yue education] engineering testing technology reference of Wenhua University](/img/69/50a8786ea062a541df9e07c1e16db5.jpg)
[Yu Yue education] engineering testing technology reference of Wenhua University

Common functions of OrCAD schematic

诸葛亮 VS 庞统,拿下分布式 Paxos
随机推荐
The e-book "action guide for large organizations to further promote zero code application platform" was officially released!
TEMPEST HDMI泄漏接收 1
Intel announced five new technological developments, including quantum computing, neural pseudo computing, machine programming, integrated optoelectronics, and secure computing
[XXL job] the pond is green and the wind is warm. I remember that Yu Zhen first met
Explain distributed raft with dynamic diagram
Mysql database import SQL file display garbled code
Icon already includes gloss effects
Escape analysis of 982 golang
TEMPEST HDMI泄漏接收 2
Alphassl wildcard certificate for one month
MySQL face Scripture eight part essay
SQL solve select basic statement
This year, I graduated
图扑软件数字孪生 3D 风电场,智慧风电之海上风电
Keepalived monitors the process and automatically restarts the service process
Omni toolbox direct download
线程状态变化涉及哪些常用 API
Debug through yalc before releasing NPM package
【批處理DOS-CMD命令-匯總和小結】-外部命令-cmd下載命令、抓包命令(wget)
I have used it for six years!