Total Accepted: 59422 Total Submissions: 213019 Difficulty: Medium

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* insertionSortList(ListNode* head) {
if(head==NULL || head->next==NULL){
return head;
}
ListNode* cur = head->next,*cur_pre=head,*cur_next=NULL;
while(cur){
cur_next = cur->next; /* 当前值小于前驱值,该节点需要重新调整 */
if(cur->val < cur_pre->val){
ListNode* insert_node_pre = NULL,*insert_node=head;
while(cur->val > insert_node->val){
insert_node_pre = insert_node;
insert_node = insert_node->next;
}
insert_node_pre ? insert_node_pre->next = cur : head = cur;
cur->next = insert_node;
cur_pre->next = cur_next;
}else{
cur_pre = cur;
} cur = cur_next;
}
return head;
}
};

[Linked List]Insertion Sort List的更多相关文章

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

    Sort a linked list using insertion sort. 链表的插入排序实现原理很简单,就是一个元素一个元素的从原链表中取出来,然后按顺序插入到新链表中,时间复杂度为O(n2) ...

  2. leetcode Insertion Sort List

    题目:Sort a linked list using insertion sort. 代码: /** * Definition for singly-linked list. * struct Li ...

  3. 【leetcode】Insertion Sort List (middle)

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

  4. 9. Sort List && Insertion Sort List (链表排序总结)

    Sort List Sort a linked list in O(n log n) time using constant space complexity.                   H ...

  5. LeetCode OJ 147. Insertion Sort List

    Sort a linked list using insertion sort. Subscribe to see which companies asked this question 解答 对于链 ...

  6. Java for LeetCode 147 Insertion Sort List

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

  7. 【LeetCode OJ】Insertion Sort List

    Problem: Sort a linked list using insertion sort. The node of the linked list is defined as: /** * D ...

  8. 147. Insertion Sort List

    Sort a linked list using insertion sort. 代码如下: /** * Definition for singly-linked list. * public cla ...

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

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

随机推荐

  1. codeforces 505B Mr. Kitayuta's Colorful Graph(水题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Mr. Kitayuta's Colorful Graph Mr. Kitayut ...

  2. PHP APC缓存配置、使用详解

    一.APC缓存简介 APC,全称是Alternative PHP Cache,官方翻译叫”可选PHP缓存”.它为我们提供了缓存和优化PHP的中间代码的框架. APC的缓存分两部分:系统缓存和用户数据缓 ...

  3. static的用法解析

    PHP中static变量的使用范围要更广一些,我们不仅可以在类,方法或变量前面添加static修饰符,我们甚至还能给函数内部变量添加static关键字.添加了static修饰符的变量即使在该函数执行完 ...

  4. 改进我们的小游戏 - 零基础入门学习Python004

    改进我们的小游戏 让编程改变世界 Change the world by program 改进我们的小游戏 很多鱼油对改善这个游戏提出了建议,小甲鱼做了一下总结,大概有以下几个方面需要改进: 猜错的时 ...

  5. 深入了解三种针对文件(JSON、XML与INI)的配置源

    深入了解三种针对文件(JSON.XML与INI)的配置源 物理文件是我们最常用到的原始配置的载体,最佳的配置文件格式主要由三种,它们分别是JSON.XML和INI,对应的配置源类型分别是JsonCon ...

  6. Eclipse 乱码解决方案(UTF8 -- GBK)

    UTF8 --> GBK;   GBK --> UTF8 eclipse的中文乱码问题,一般不外乎是由操作系统平台编码的不一致导致,如Linux中默认的中文字体编码问UTF8, 而Wind ...

  7. Tea加密算法和XxTea加密算法

    TEA(Tiny Encryption Algorithm)是一种小型的对称加密解密算法,支持128位密码,与BlowFish一样TEA每次只能加密/解密8字节数据.TEA特点是速度快.效率高,实现也 ...

  8. 虚拟机比较(wiki)

    https://zh.wikipedia.org/wiki/%E8%99%9B%E6%93%AC%E6%A9%9F%E5%99%A8#.E8.99.9B.E6.93.AC.E6.A9.9F.E5.99 ...

  9. Android EditText圆角的方法

    一.在drawable下面添加xml文件rounded_editview.xml <?xml version="1.0" encoding="utf-8" ...

  10. 一次使用Eclipse Memory Analyzer分析Tomcat内存溢出

    转:http://tivan.iteye.com/blog/1487855 前言 在平时开发.测试过程中.甚至是生产环境中,有时会遇到OutOfMemoryError,Java堆溢出了,这表明程序有严 ...