美文网首页
Java实现单链表排序

Java实现单链表排序

作者: 糖醋排骨盐酥鸡 | 来源:发表于2019-03-19 21:15 被阅读0次

今天复习数据结构,想用单链表实现插入排序,在网上查资料,发现大部分都需要新建一个链表进行插入,感觉空间复杂度过高,便借用单链表原地反转的思想。

// 插入排序

public void insertSortNode() {

if (Length() < 2) {

System.out.println("无需排序");

return;

}

Node temp = head;

Node pNode = head.next.next;

Node qNode = pNode.next;

temp.next.next = null;

while (qNode != null) {

pNode.next = null;

while (temp.next != null) {

if ((int) pNode.data < (int) temp.next.data) {

pNode.next = temp.next;

temp.next = pNode;

pNode = qNode;

qNode = qNode.next;

print();

break;

}

temp = temp.next;

}

if (temp.next == null) {

temp.next = pNode;

pNode = qNode;

qNode = qNode.next;

}

}

if (qNode == null) {

pNode.next = null;

while (temp.next != null) {

if ((int) pNode.data < (int) temp.next.data) {

pNode.next = temp.next;

temp.next = pNode;

print();

break;

}

temp = temp.next;

}

if (temp.next == null) {

temp.next = pNode;

}

}

}

相关文章

  • 链表

    单链表 C实现 Java实现 双链表 C实现 Java实现

  • java单链表排序

    在线手写 java单链表排序 单链表每个节点为: 如果一个单链表为2->1->3->5->4,经过排序后链表结构为...

  • Java实现单链表排序

    今天复习数据结构,想用单链表实现插入排序,在网上查资料,发现大部分都需要新建一个链表进行插入,感觉空间复杂度过高,...

  • 面试:用 Java 逆序打印链表

    面试:用 Java 逆序打印链表 昨天的 Java 实现单例模式 中,我们的双重检验锁机制因为指令重排序问题而引入...

  • 有关算法的面试题收集

    1. 对单链表排序,用代码实现【腾讯】 2. 快速找到未知长度的单链表的中间节点【腾讯】 普通方法:遍历一遍单链表...

  • 单链表实现直接插入排序(JAVA)

    去面试问了单链表实现快排的问题,所以想来把八大排序算法的单链表实现总结一下。 这篇就先总结直接插入排序。实际上,算...

  • 数据结构 | 其二 链表

    冰河winner - 数据结构之链表 2.1 单向链表 数据结构(一) 单链表的实现-JAVA 2.2 双端链表 ...

  • 单链表快排

    单链表快速排序 - Jensen抹茶喵 - 博客园 图 单链表的快速排序 - CSDN博客

  • 单向链表算法

    单向链表 反转单向链表 单链表查找倒数第k个节点 单链表递归倒序打印 单链表排序 单链表删除重复节点

  • Message.obtain()中的单链表栈缓存

    Message.obtain()中的单链表栈缓存 Android中的Message.java用单链表实现了一个si...

网友评论

      本文标题:Java实现单链表排序

      本文链接:https://www.haomeiwen.com/subject/bhlomqtx.html