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. zoj 2524 并查集裸

    Description There are so many different religions in the world today that it is difficult to keep tr ...

  2. 哈希表(Hash Map)

    今天第一次做Leetcode用到了散列表,之前学的数据结构的内容都忘了,正好趁热打铁补一补. 摘自其他博客的一个整合. 一.哈希表简介 数据结构的物理存储结构只有两种:顺序存储结构和链式存储结构(像栈 ...

  3. 今天在2cto网站看到一个有关try{}catch(){}finally{}语句中含有return的讲解,理解很透彻。

    publicclassTrycatchTest{ publicstaticvoidmain(String[]args){ System.out.println("x:"+newTr ...

  4. python glob模块使用笔记(更新)

    glob模块是通配用的,用于列出符合通配格式的文件 glob.glob(path) path是用于匹配的字符串,类似简单版的正则吧 其中 * 匹配任意多个字符 ? 匹配一个字符 [1-9] 匹配指定范 ...

  5. 21. Wireless tools (无线工具 5个)

    AircrackKismetNetStumblerinSSIDerKisMAC

  6. 用vs2010打开使用vs2013升级后的WP工程

    项目在win7+vs2010的环境中建立的,后来在win8.1+vs2013的环境下修改和完善: 但是所有功能实现后发现wp7项目在使用vs2013打开后因为单向升级的原因,项目只能被编译为wp8项目 ...

  7. C语言学习笔记之位运算求余

    我们都知道,求一个数被另一个数整除的余数,可以用求余运算符”%“,但是,如果不允许使用求余运算符,又该怎么办呢?下面介绍一种方法,是通过位运算来求余,但是注意:该方法只对除数是2的N次方幂时才有效. ...

  8. dic and set

    一.dic 1.格式:key:value 2.key值必须不可变(可hash) 3.key不可重复(唯一性) 4.优点:查找.插入速度快 5.缺点:空间消耗大 6.实质是以空间换速度 7.常用参数 1 ...

  9. Dubbo 入门学习笔记

    项目结构 模块介绍: DubboAPI    ----API接口 DubboConsumer ----消费者 DubboProvider ----生产者 DubboAPI  Service 提供的接口 ...

  10. Centos6.9部署Gitlab-11.9.8并汉化

    Git 是一种分布式的代码版本管理系统,git在工作时可以不用时刻依赖后台服务器,在本地电脑上就可以管理版本控制,但是在需要协同开发时就必须要使用后台服务器了,目前互联网上有github,码云这样的远 ...