Sort a linked list using insertion sort.

A graphical example of insertion sort. The partial sorted list (black) initially contains only the first element in the list.
With each iteration one element (red) is removed from the input data and inserted in-place into the sorted list

Algorithm of Insertion Sort:

 Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list.
At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there.
It repeats until no input elements remain.

Example 1:

Input: ->->->
Output: ->->->

Example 2:

Input: -->->->->
Output: -->->->->

解题思路:将原链表中元素一个一个取出来,在新链表中比较进行排序。时间复杂度为O(n2),是一种效率并不是很高的算法,但是空间复杂度为O(1),以高时间复杂度换取了低空间复杂度。

方法一(C++)

 ListNode* insertionSortList(ListNode* head) {
ListNode* dummy=new ListNode(-),*cur=dummy;
while(head){
ListNode* t=head->next;
cur=dummy;
while(cur->next&&cur->next->val<=head->val)
cur=cur->next;
head->next=cur->next;
cur->next=head;
head=t;
}
return dummy->next;
}

(Java)

  public ListNode insertionSortList(ListNode head) {
ListNode dummy=new ListNode(-1),cur=dummy;
while(head!=null){
ListNode t=head.next;
cur=dummy;
while(cur.next!=null&&cur.next.val<=head.val)
cur=cur.next;
head.next=cur.next;
cur.next=head;
head=t;
}
return dummy.next;
}

方法二:不符合题目中的要求,只是完成最基本的链表排序功能,借助vector中的sort排序方法。(C++)

  ListNode* insertionSortList(ListNode* head) {
vector<int> m;
while(head){
m.push_back(head->val);
head=head->next;
}
sort(m.begin(),m.end());
ListNode* newhead=new ListNode(-);
while(!m.empty()){
ListNode* cur=new ListNode(m.back());
m.pop_back();
cur->next=newhead->next;
newhead->next=cur;
}
return newhead->next;
}

LeetCode 147. Insertion Sort List 链表插入排序 C++/Java的更多相关文章

  1. [LeetCode] 147. Insertion Sort List 链表插入排序

    Sort a linked list using insertion sort. A graphical example of insertion sort. The partial sorted l ...

  2. 147 Insertion Sort List 链表插入排序

    用插入排序对链表进行排序. 详见:https://leetcode.com/problems/insertion-sort-list/description/ Java实现: 链表的插入排序实现原理很 ...

  3. [LeetCode]147. Insertion Sort List链表排序

    插入排序的基本思想 把排好的放在一个新的变量中,每次拿出新的,排进去 这个新的变量要有超前节点,因为第一个节点可能会有变动 public ListNode insertionSortList(List ...

  4. [LeetCode] Insertion Sort List 链表插入排序

    Sort a linked list using insertion sort. 链表的插入排序实现原理很简单,就是一个元素一个元素的从原链表中取出来,然后按顺序插入到新链表中,时间复杂度为O(n2) ...

  5. [LeetCode] 147. Insertion Sort List 解题思路

    Sort a linked list using insertion sort. 问题:实现单向链表的插入排序. 这是比较常规的一个算法题目. 从左往右扫列表,每次将指针的下一个元素插入前面已排好序的 ...

  6. Java for LeetCode 147 Insertion Sort List

    Sort a linked list using insertion sort. 解题思路: 插入排序,JAVA实现如下: public ListNode insertionSortList(List ...

  7. leetcode 147. Insertion Sort List ----- java

    Sort a linked list using insertion sort. 插入排序. /** * Definition for singly-linked list. * public cla ...

  8. Leetcode#147 Insertion Sort List

    原题地址 心得:有关链表的题目,多用中间变量,代码写得清晰一点,适当注释 代码: ListNode *insertionSortList(ListNode *head) { if (!head) re ...

  9. 【数据结构】算法 LinkList (Insertion Sort List 链表插入排序)

    将一个单链表进行处理后,所得结果为一有序链表 Solution: 将原始链表逐个查询,插入新链表,在插入的同时对链表进行排序.时间复杂度O(n*n) public ListNode insertion ...

随机推荐

  1. 学习笔记(三)--Lucene分词器详解

    Lucene-分词器API org.apache.lucene.analysi.Analyzer 分析器,分词器组件的核心API,它的职责:构建真正对文本进行分词处理的TokenStream(分词处理 ...

  2. 芯灵思Sinlinx A64开发板设置qt程序自启动

    开发平台 芯灵思Sinlinx A64 内存: 1GB 存储: 4GB 开发板详细参数 https://m.tb.cn/h.3wMaSKm 对于开发板开机启动程序的设置可以这样做通过串口连接开发板 v ...

  3. Spring MVC的原理及配置详解

    网址链接:https://www.cnblogs.com/baiduligang/p/4247164.html

  4. Azure DevOps Server/Team Foundation Server

    TFS wasn't designed specifically to support a requirements management process. Epics are like big st ...

  5. android 位置记录软件

    行者 用的百度高德的方案,没有偏移问题endomondo,咕咚,行者.endomondo是国外软件,运行稳定,但GPS记录漂移比较严重:咕咚的GPS位置记录比较准确,缺点是容易崩溃,譬如记录过程中来个 ...

  6. [JAVA]JAVA实现多线程的三种方式

    1.继承Thread类,通过start()方法调用 public class MultiThreadByExtends extends Thread { @Override public void r ...

  7. 解决httpclient的NoHttpResponseException异常

    https://blog.csdn.net/liubenlong007/article/details/78180333

  8. Opencv + opencv_contrib + Tesseract 之Qt开发环境搭建

    1.软件包准备 opencv源码包地址:                官网  github opencv_contrib源码包地址:   github Tesseract源码包地址:        ...

  9. Springboot+ActiveMQ(ActiveMQ消息持久化,保证JMS的可靠性,消费者幂等性)

    ActiveMQ 持久化设置: 在redis中提供了两种持久化机制:RDB和AOF 两种持久化方式,避免redis宕机以后,能数据恢复,所以持久化的功能 对高可用程序来说 很重要. 同样在Active ...

  10. 好玩的PIL

    原图(下面的代码全为部分)不喜欢的一个库 缩小的代码 from PIL import Image im=Image.open('tupian.jpg') im.thumbnail(128,128)) ...