题目:

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) {
ListNode *p1 = head;
ListNode dummy(INT_MIN);
while (p1)
{
ListNode *tmp1 = p1->next;
ListNode *p2 = &dummy;
while ( p2->next )
{
if ( p2->next->val > p1->val )
{
ListNode *tmp2 = p2->next;
p2->next = p1;
p1->next = tmp2;
break;
}
else
{
p2 = p2->next;
}
}
if (!p2->next)
{
p2->next = p1;
p1->next = NULL;
}
p1 = tmp1;
}
return dummy.next;
}
};

tips:

插入排序算法在链表上的实现。

1. 设立一个虚表头dummy,虚表头后面接的就是已经排序好的部分ListNodes

2. 维护一个指针p1,始终指向待插入的ListNode

3. 里层的while循环需要选择插入的具体位置

=============================================

第二次过这道题,一次AC。

/**
* 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) {
ListNode dummpy(INT_MIN);
while ( head )
{
ListNode* tmp = head->next;
ListNode* pre = &dummpy;
ListNode* curr = dummpy.next;
while ( curr )
{
if ( head->val<curr->val)
{
pre->next = head;
head->next = curr;
break;
}
else
{
pre = curr;
curr = curr->next;
}
}
if ( !curr )
{
pre->next = head;
head->next = NULL;
}
head = tmp;
}
return dummpy.next;
}
};

【Insertion Sorted List】cpp的更多相关文章

  1. 【Merge Sorted Array】cpp

    题目: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Not ...

  2. 【Merge K Sorted Lists】cpp

    题目: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexit ...

  3. 【Merge Two Sorted Lists】cpp

    题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splici ...

  4. 【Remove Duplicates from Sorted List 】cpp

    题目: 第一次刷的时候漏掉了这道题. Given a sorted linked list, delete all duplicates such that each element appear o ...

  5. 【Median of Two Sorted Arrays】cpp

    题目: There are two sorted arrays A and B of size m and n respectively. Find the median of the two sor ...

  6. 【Search In Rotated Sorted Array】cpp

    题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7  ...

  7. 【Remove Duplicates from Sorted Array】cpp

    题目: https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Given a sorted array, remove ...

  8. 【Search Insert Position 】cpp

    题目: Given a sorted array and a target value, return the index if the target is found. If not, return ...

  9. hdu 4740【模拟+深搜】.cpp

    题意: 给出老虎的起始点.方向和驴的起始点.方向.. 规定老虎和驴都不会走自己走过的方格,并且当没路走的时候,驴会右转,老虎会左转.. 当转了一次还没路走就会停下来.. 问他们有没有可能在某一格相遇. ...

随机推荐

  1. Face++云相册应用IOS源码

    该源码是一个不错的相册应用,Face++云相册应用源码,以人脸识别作为用户注册和登录的依据,登录后可以进入用户的云相册空间,并对相册进行上传图片或删除图片,另添加了分享功能. <ignore_j ...

  2. POJ C程序设计进阶 编程题#3:运算符判定

    编程题#3:运算符判定 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 两个 ...

  3. 在Linux命令行窗口中,怎么向上翻页?

    解决方法:本机环境:vmware linux Redhat9.0版本 使用:Shift + PageUp 和 Shift + PageDown向上和向下翻页

  4. win2003以isapi的方式配置php+mysql环境(安装了shopEX)

    一.准备相关组件 mysql-installer-community-5.5.29.0.zip php-5.2.17-Win32-VC6-x86 ZendOptimizer-3.3.3-Windows ...

  5. PHP导出excel文件

    现在教教你如何导入excel文件: 在我的文件储存里面有一个com文件夹的,将其解压放在ThinkPHP/Library/文件夹里面,然后就是写控制器啦!去调用这个插件: <?php names ...

  6. 在Activity中设置new出来的TextView属性

    //创建一个TextView---->textView TextView textView = new TextView(this);   // 第一个参数为宽的设置,第二个参数为高的设置 te ...

  7. python 小技巧(import模块、查询类继承关系、安装包)

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 在这里列举一些我使用Python时积累的小技巧.这些技巧是我在使用Python过程 ...

  8. hdu 1008

    题目意思是:给你N个数字  每个数字表示多少层楼  现在要你从0层楼开始坐电梯  一次按顺序走过这些楼层 规则是 上楼6秒 ,下楼4秒,每次到达一个楼层停5秒..... 思路:模拟 代码如下:(要注意 ...

  9. CK表达式编辑器

    1.      什么是表达式编辑器? 这个工具允许技术员传入一系列的参数,由用户编辑一个公式返回一种特定的结果.之所以需要使用表达式编辑器,就是因为用户编辑的公式经常变,技术员无法想出一办法来适应用户 ...

  10. mongoDB 3.0 安全权限访问控制

    MongoDB3.0权限,啥都不说了,谷歌百度出来的全是错的.先安装好盲沟,简单的没法说. 首先,不使用 —auth 参数,启动 mongoDB: mongodb-linux-i686-3.0.0/b ...