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. //NSUserDeafult 图片的保存与读取

    //NSUserDeafult保存图片数据到本地 -(void)saveImage:(UIImage *)image{ NSData* data=[NSKeyedArchiver archivedDa ...

  2. Webform服务器控件调用JS

    服务器控件调用JS一.两类JS的触发设计1.提 交之前的JS -- 加js的事件C#处理程序2.提交之后的JS -- 用C#代码向页面上写<script>..</script> ...

  3. SPOJ QTREE 系列解题报告

    题目一 : SPOJ 375 Query On a Tree http://www.spoj.com/problems/QTREE/ 给一个树,求a,b路径上最大边权,或者修改a,b边权为t. #in ...

  4. codeforces 336D Vasily the Bear and Beautiful Strings(组合数学)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Vasily the Bear and Beautiful Strings Vas ...

  5. python-整理--pip whl命令

    如果要在windows系统上安装新的包,可以下载*.exe安装文件,双击下一步...,如果找不到exe的话. 在CMD中执行 pip install 安装包文件.whl 就可以安装了 pip这个命令本 ...

  6. Linux 下配置 SoftEther Client

    我经常使用的代理有 SSH, GoAgent, FreeGate, VPN, HttpProxy 等等,不过 SoftEther 应该是我用过的最快.最稳定的 VPN 协议. Windows 下配置 ...

  7. linux ar 命令的使用说明那个和例子[转]

    用途说明 创建静态库.a文件.用C/C++开发程序时经常用到,但我很少单独在命令行中使用ar命令,一般写在makefile中,有时也会在shell脚 本中用到.关于Linux下的库文件.静态库.动态库 ...

  8. centos搭建zabbix

    安装一些依赖包,不同情况缺的包不同 yum install mysql-devel curl curl-devel net-snmp net-snmp-devel perl-DBI php-gd ph ...

  9. 使用ARM模板部署自动扩展的Linux VMSS(2)

    12.准备完了模板文件,我们使用Powershell来创建VMSS for Linux的自动扩展集合,首先登陆到Azure中国的ARM账号: Login-AzureRmAccount -Environ ...

  10. 伪静态(URL重写)

    伪静态在可以使用数据库提供更强大的功能的同时,将很长很复杂的链接变成简短的静态链接形式,迎合搜索引擎方便搜索引擎蜘蛛(Spider)来抓取网页上的相关内容,提高页面被搜索引擎索引收录的比率,为用户提供 ...