题目:

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. 使用Visual Studio Code开发Asp.Net Core WebApi学习笔记(六)-- 依赖注入

    本篇将介绍Asp.Net Core中一个非常重要的特性:依赖注入,并展示其简单用法. 第一部分.概念介绍 Dependency Injection:又称依赖注入,简称DI.在以前的开发方式中,层与层之 ...

  2. 一些peoplecode小技巧平【二】

    1. Set component changed page field property: For understanding this open a page in application desi ...

  3. 最近的学习的linux命令笔记

    vmstat 2 2 mail -s nihao root < test.txt     f 323   h 300-310 crontab  -l,-e,-r    vim /etc/cron ...

  4. mysql给root开启远程访问权限,修改root密码

    1.MySql-Server 出于安全方面考虑只允许本机(localhost, 127.0.0.1)来连接访问. 这对于 Web-Server 与 MySql-Server 都在同一台服务器上的网站架 ...

  5. php 数组排序代码

    php对数组排序代码.   <?phpclass='pingjiaF' frameborder='0' src='http://www.jbxue.com' scrolling='no'> ...

  6. Creating an API-Centric Web Application[转]

    Creating an API-Centric Web Application 转自 http://hub.tutsplus.com/tutorials/creating-an-api-centric ...

  7. Laravel 5 基础(十一)- 表单验证

    在建立一个文章的时候,如果你什么都不输入直接提交,ok,你获得了一个空的文章,没有任何错误提示,这是不对的.在命令行下运行 php artisan 可以看到一个选项 make:request,新建一个 ...

  8. C#创建、读取和修改Excel

    // Namespaces, Variables, and Constants using System; using System.Configuration; using System.Data; ...

  9. 委托、匿名函数与Lambda表达式初步

    (以下内容主要来自<C#本质论第三版>第十二章委托和Lambda表达式) 一.委托续 上上周五看了看委托,初步明白了其是个什么,如何定义并调用.上周五准备看Lambda表达式,结果发现C# ...

  10. [terry笔记]Flashback

    flashback 闪回,主要功能有两个:闪回查询(flashback query).闪回恢复(flashback table/database) 开启闪回flashback功能(归档下才可开启数据库 ...