Sort a linked list using insertion sort.

题目要求:链表的插入排序,由于没有时间复杂度的要求,可以直接循环操作。

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* insert(ListNode* head,int num){
ListNode *node = new ListNode(num);
if(head==NULL || num <= head->val){
node->next=head;
return node;
} ListNode *pre = head;
ListNode *tail = head->next;
while(tail!=NULL && num > tail->val){ // 一定要注意tail!=NULL 先决条件
pre= tail;
tail=tail->next;
}
node->next = pre->next;
pre->next = node;
return head; }
ListNode* insertionSortList(ListNode *head){
if(head==NULL || head->next==NULL) return head;
ListNode *tmp = head;
ListNode* ans=NULL;
while(tmp!=NULL){
ans = insert(ans,tmp->val);
tmp =tmp->next;
}
return ans;
}
};

转载请注明出处: http://www.cnblogs.com/double-win/ 谢谢!

[LeetCode 题解]: Insertion Sort List的更多相关文章

  1. [Leetcode Week16]Insertion Sort List

    Insertion Sort List 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/insertion-sort-list/description/ ...

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

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

  3. 【leetcode】Insertion Sort List (middle)

    Sort a linked list using insertion sort. 思路: 用插入排序对链表排序.插入排序是指每次在一个排好序的链表中插入一个新的值. 注意:把排好序的部分和未排序的部分 ...

  4. Java for LeetCode 147 Insertion Sort List

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

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

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

  6. leetcode:Insertion Sort List

    Sort a linked list using insertion sort. 分析:此题要求在链表上实现插入排序. 思路:插入排序是一种O(n^2)复杂度的算法,基本想法就是每次循环找到一个元素在 ...

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

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

  8. leetcode 名单 Insertion Sort List

    Insertion Sort List Total Accepted: 24444 Total Submissions: 96639My Submissions Sort a linked list ...

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

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

随机推荐

  1. 批处理运行Python脚本

    创建 在某个目录下创建script.bat文件 比如在D:\Code\VimCode\Python_auto\Script中 编辑script.bat文件 写入 @python.exe D:\Code ...

  2. Newtonsoft.Json(Json.Net)学习

    转自原文 Newtonsoft.Json(Json.Net)学习笔记 Newtonsoft.Json,一款.NET中开源的Json序列化和反序列化类库.软件下载地址: http://www.newto ...

  3. mongodb分片(七)

    1.插入负载技术分片架构图 2.片键的概念和用处 看下面这个普通的集合和分片后的结果 3.什么时候用到分片呢? 3.1机器的磁盘空间不足 3.2单个的mongoDB服务器已经不能满足大量的插入操作 3 ...

  4. leetcode558

    """ # Definition for a QuadTree node. class Node(object): def __init__(self, val, isL ...

  5. JAVA压缩 解压缩zip 并解决linux下中文乱码

    1. [代码][Java]代码   1:再压缩前,要设置linux模式, 需要使用第三方ant-1.6.5.jar  如果是文件目录,则ZipEntry zipEntry=new ZipEntry(b ...

  6. postgresql 相关

    http://www.yiibai.com/html/postgresql/2013/080998.html 1.安装PG的client以及函数库: yum install postgresql    ...

  7. golang的array/slice

    相同点 由相同类型的元素组合构成 元素有序排列,0为第一个元素下标 基本使用方法相同 区别 array声明时需要指定容量大小,而且无法修改 slice可通过append增加元素,当容量不够时,会自动扩 ...

  8. 认识Excel并创建一个excel(网址:http://poi.apache.org/)

    需要导入的jar包: package com.huawei.excel; import java.io.FileOutputStream; import org.apache.poi.hssf.use ...

  9. python 数值计算库

    pip install numpy pip install matplotlib pip install sklearn yum -y install tkinter pip install scip ...

  10. Android开发实战之简单音乐播放器

    最近开始学习音频相关.所以,很想自己做一个音乐播放器,于是,花了一天学习,将播放器的基本功能实现了出来.我觉得学习知识点还是蛮多的,所以写篇博客总结一下关于一个音乐播放器实现的逻辑.希望这篇博文对你的 ...