LeetCode 147. Insertion Sort List 链表插入排序 C++/Java

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:

1 Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list.
2 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.
3 It repeats until no input elements remain.

Example 1:

Input: 4->2->1->3
Output: 1->2->3->4

Example 2:

Input: -1->5->3->4->0
Output: -1->0->3->4->5

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

方法一(C++)

 1 ListNode* insertionSortList(ListNode* head) {
 2         ListNode* dummy=new ListNode(-1),*cur=dummy;
 3         while(head){
 4             ListNode* t=head->next;
 5             cur=dummy;
 6             while(cur->next&&cur->next->val<=head->val)
 7                 cur=cur->next;
 8             head->next=cur->next;
 9             cur->next=head;
10             head=t;
11         }
12         return dummy->next;
13     }

(Java)

 1  public ListNode insertionSortList(ListNode head) {
 2         ListNode dummy=new ListNode(-1),cur=dummy;
 3         while(head!=null){
 4             ListNode t=head.next;
 5             cur=dummy;
 6             while(cur.next!=null&&cur.next.val<=head.val)
 7                 cur=cur.next;
 8             head.next=cur.next;
 9             cur.next=head;
10             head=t;
11         }
12         return dummy.next;
13     }

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

 1  ListNode* insertionSortList(ListNode* head) {
 2         vector<int> m;
 3         while(head){
 4             m.push_back(head->val);
 5             head=head->next;
 6         }
 7         sort(m.begin(),m.end());
 8         ListNode* newhead=new ListNode(-1);
 9         while(!m.empty()){
10             ListNode* cur=new ListNode(m.back());
11             m.pop_back();
12             cur->next=newhead->next;
13             newhead->next=cur;
14         }
15         return newhead->next;
16     }

posted on 2019-03-29 10:58 木落长安rr 阅读(...) 评论(...) 编辑 收藏

转载于:https://www.cnblogs.com/hhhhan1025/p/10619875.html

LeetCode 147. Insertion Sort List 链表插入排序 C++/Java相关推荐

  1. LeetCode 147. Insertion Sort List

    LeetCode 147. Insertion Sort List Solution1:我的答案 有点笨,有点慢 /*** Definition for singly-linked list.* st ...

  2. [Leetcode]147. Insertion Sort List

    Sort a linked list using insertion sort. 链表的插入排序 思路,递归到链表尾,然后循环插入: 1 /** 2 * Definition for singly-l ...

  3. LeetCode OJ 147. Insertion Sort List

    Sort a linked list using insertion sort. 对链表使用插入排序还是很简单的,从链表中拆下一个节点,然后把它插入到已经排序的部分的链表中,直到所有节点都被插入.代码 ...

  4. 147. Insertion Sort List

    /** 147. Insertion Sort List * 2016-6-1 by Mingyang * insertion sort的基本思路要有,两个while循环,一次过,对于每一个值与pre ...

  5. 【leetcode】147. Insertion Sort List

    Sort a linked list using insertion sort. 链表的插入排序. 需要创建一个虚拟节点.注意点就是不要节点之间断了. class Solution { public: ...

  6. leetcode之Insertion Sort List

    Sort a linked list using insertion sort. 这道题是用链表做插入排序.虽然之前数据结构中学习了链表的插入删除等,但那些都是最简单的基本操作,只是给出一个节点,直接 ...

  7. 173. Insertion Sort List【LintCode by java】

    Description Sort a linked list using insertion sort. Example Given 1->3->2->0->null, ret ...

  8. insertion Sort List (链表的插入排序) leecode java

    逻辑简单,代码难写,基础不劳,leecode写注释不能出现中文,太麻烦,我写了大量注释,链表问题最重要的就是你那个指针式干啥的 提交地址https://oj.leetcode.com/problems ...

  9. 【LeetCode笔记】142. 环形链表 II(Java、快慢指针)

    文章目录 题目描述 思路 & 代码 更新版 三刷 - 再更新 题目描述 相对于环形链表,这里要求找到环的起点 难点在于 O(1),否则可以直接哈希表冲 思路 & 代码 找出快慢指针的路 ...

最新文章

  1. 二.Springboot 常用注解
  2. 使用Apache Storm和Kite SDK Morphlines的可配置ETL处理
  3. mysql myisam 分表_mysql myisam简单分表设计
  4. 二叉排序树的查找、插入
  5. 字节跳动面试流程和考点都在这儿
  6. CentOS minimal 版安装图形界面的步骤分享,中文语言包
  7. Struts2的标签库(二)之数据标签
  8. UMl user guide读书笔记
  9. 打开.class文件
  10. 电子计算机的指令是由什么和什么组成的,吴承亮问:计算机指令由两部分组成它们是 计算机指令由哪两个字段组成,各自的作用是什么?...
  11. java开发必备基础
  12. 2012年信息系统项目管理师下半年上午考试习题与答案解析
  13. AirPods声音越来越小问题
  14. 让游戏在英国取得成功的 5 个技巧
  15. 【Python】python之subprocess模块详解
  16. 服务器突然Out of memory的问题排查
  17. 如何在Github中添加图片
  18. PyTorch 数据归一化与反归一化
  19. 中国IT成功人士特点6大成功密码全解析
  20. 关于解压 tar.gz的问题

热门文章

  1. 【问链财经-区块链基础知识系列】 第二十二课 贸易金融区块链平台的技术机理与现实意义
  2. 语音聊天室 html,聊天室 - HTML - php中文网博客
  3. 试用去水印_教你一键视频去水印,支持抖音、快手、小红书、哔哩哔哩等几十个平台...
  4. php 运行 shell命令行参数,PHP exec()在通过浏览器执行时不会执行shell命令
  5. Cortex-A 的内核寄存器组
  6. SQLite自增关键字报错(near “AUTO_INCREMENT“: syntax error)
  7. php 文件上传页面模板,PHP文件上传类实例详解
  8. 用php编写比赛评奖系统_php编写的抽奖程序中奖概率算法
  9. 破解密码高手王小云教授简介
  10. 数列分段pascal程序