题目:

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. 实例分析ELF文件静态链接

    参考文献: <ELF V1.2> <程序员的自我修养---链接.装载与库>第4章 静态链接 开发平台: [thm@tanghuimin static_link]$ uname ...

  2. jquery中选择ID以什么字符开头的匹配主要用于多个上传控件的时候,id无法使用,而且class不起作用的时候

    $("[id^=remark]")选择ID以remark开头的所有数据进行匹配

  3. Linux使用Shell脚本实现ftp的自动上传下载

    1. ftp自动登录批量下载文件. #####从ftp服务器上的/home/data 到 本地的/home/databackup#####!/bin/bashftp -n<<!open 1 ...

  4. 【Winform】DataTable绑定到ComboBox

    我们从数据库中查询出来的数据存放在Datatable中 1.DataTable绑定到ComboBox上 cmbRole.DataSource = datatable; cmbRole.DisplayM ...

  5. Ueditor防止代码自动清除

    Ueditor功能真的很牛逼,可也有让人悲催的地方,尤其是自动清除代码,会将你默认的div标签改成p,挺让人闹心的,不过Ueditor的开发人员还是满热心的,搜遍网上无答案的时候,问了下他们,解决了 ...

  6. 管理员 修改MySQL 5.7.9 新版本的root密码方法以及一些新变化整理

    MySQL 5.7版本开始,增强密码验证机制,网上说安装的时候会在/root/.mysql_secret  文件中生成默认密码,这一点自 5.7.6版本以后也去掉了. 针对如果生成默认密码,网上有一个 ...

  7. xml结构

    一.XmlHelper using System; using System.Collections.Generic; using System.Linq; using System.Web; usi ...

  8. UART,USART,SPI,I2C等总线的介绍与区别20160526

    首先来说一下UART和USART的区别: 1.字面意义: UART:universal asynchronous receiver and transmitter通用异步收发器: USART:univ ...

  9. python 核心编程第二版 课后习题 第11章

    11-3 函数.在这个练习中,我们将实现 max()和 min()内建函数. (a) 写分别带两个元素返回一个较大和较小元素,简单的 max2()核 min2()函数.他们应该可以用任意的 pytho ...

  10. desin pattern

    uml tool http://cruise.site.uottawa.ca/umple/ http://www.umldesigner.org/download/ http://www.eclips ...