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. java————数组 简单写出一个管理系统

    数组的特点 1,  数组是一块连续的空间,下标描述空间的位置. 2,  下标从0开始,最大下标为数组长度—1.(*.length-1) 3,  数组元素都是变量.(就是每个下标对应的内容).变量的类型 ...

  2. Redis事务和实现秒杀功能的实现

    今天带着学生学习了Redis的事务功能,Redis的事务与传统的关系型数据库(如MySQL)有所不同,Redis的事务不能回滚. Redis中使用multi.exec.discard.watch.un ...

  3. EXCEL中,如何引用一个单元格中的数据,作为另一个单元格内容中的一部分?

    https://zhidao.baidu.com/question/230715654.html 假设单元格A1值是8(该值由函数计算得出),我要在单元格B1中引用A1的值,但只是作为B1单元格内容中 ...

  4. xc笔记

    2019-03-20正式开始准备 --言语理解与表达------------------------------------------------------- 分为 1.逻辑填空   2.片段阅读 ...

  5. Vue字符串传入变量

  6. chrony配置的和相关命令

    ntp命令 查看时间同步源: $ chronyc sources -v 查看时间同步源状态: $ chronyc sourcestats -v 设置硬件时间 硬件时间默认为UTC: $ timedat ...

  7. HTTP协议初步解析

    一.什么是HTTP协议 HTTP是hypertext transfer protocol(超文本传输协议)的简写,它是TCP/IP协议的一个应用层协议,定义了Web客户端向Web服务器请求Web页面的 ...

  8. 数据库if判断语句

    THEN '青年' ELSE '未成年' END) as age_text from user 更多写法参考:http://www.cnblogs.com/martinzhang/p/3220595. ...

  9. LINUX系统软件安装和卸载的常见方法

    linux系统分很多种简单介绍几种常用的: 1.centos/redhat: 安装: rpm安装,如果有依赖,很闹心,如果使用--nodeps不检查依赖,会有问题. #rpm -ivh <XXX ...

  10. final link failed: Nonrepresentable section on output

    编译live555的时候遇到了这个问题,前面的编译没有问题,是在链接的时候出现的,在网上搜索说是缺少 libstdc++ 库.于是,安装之 #sudo apt-get install  libstdc ...