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. 找不到 org/springframework/dao/support/PersistenceExceptionTranslator

    如果用的spring2  则原因是缺少spring-dao.jar 如果用的是spring3(我就栽这儿了) 则原因是缺少org.springframework.transaction-3.0.4.R ...

  2. 【314】putty 自动登录

    putty是一款好用的远程登录linux服务器软件,但每次输入用户名密码毕竟有些烦人,这里教你免用户名密码登陆. 本教程通过 *.bat 文件进行添加参数,下面为相应的代码: 方法一:(直接将密码/用 ...

  3. EF中关系映射问题

    一对一,和一对多的简单问题就部说了,直接来多对多这样的问题吧. 首现关系映射为这样的: /// <summary> /// 对应数据库中dbo.Address表 /// </summ ...

  4. 【原】Coursera—Andrew Ng机器学习—Week 7 习题—支持向量机SVM

    [1] [2] Answer: B. 即 x1=3这条垂直线. [3] Answer: B 因为要尽可能小.对B,右侧红叉,有1/2 * 2  = 1 ≥ 1,左侧圆圈,有1/2 * -2  = -1 ...

  5. nodejs开发工具

    我选择的是Hbuilder作为node项目的开发工具.   先在Hbuilder 里面安装nodeEclipse插件,然后重启工具. 点击添加项目,选择其他选项,出现下图选项,然后选择圈住的选项点击下 ...

  6. Force SDK to create Appointment programmatically in crm 2013 onPremise

    As I know based on this: msdn.microsoft.com/.../gg334289.aspx and community.dynamics.com/.../book-an ...

  7. Culling & Depth Testing

    [Culling & Depth Testing] Culling is an optimization that does not render polygons facing away f ...

  8. Python——字典与字典方法

    字典是一种通过名字或者关键字引用的得数据结构,其键可以是数字.字符串.元组,这种结构类型也称之为映射.字典类型是Python中唯一內建的映射类型,基本的操作包括如下: (1)len():返回字典中键— ...

  9. Activiti 乱码问题

    新版本运行起来中文看起来正常了许多,至少生成图片过程中不会出现乱码了,但不出预料的再次遇到部署时乱码问题,除了保存时报错,还会导致流程实例的节点名称是乱码. 1. IE报错定义时错误提示: 元素类型 ...

  10. 启动日志中就出现[java:comp/env/spring.liveBeansView.mbeanDomain] not found这个日志

    今天在做一个dubbo服务端的时候,启动成功,dubbo也注册成功,但是启动日志中就出现[java:comp/env/spring.liveBeansView.mbeanDomain] not fou ...