题目:

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. 搭建高性能计算环境(一)、Linux操作系统的安装和配置

    一般课题组刚开始做计算,往往没有专门的集群,主要用自己的PC机.工作站或者买几台服务器来跑跑:小伙伴们摸索Linux的使用.编译一些开源软件.甚至写点Shell脚本需要耗费很多时间,耽搁了读文献.码论 ...

  2. linux禁止tty终端登陆

    修改文件/etc/pam.d/system-auth #%PAM-1.0# This file is auto-generated.# User changes will be destroyed t ...

  3. CentOS 5.x版本升级Mysql

    #-----------------------------CentOS 5.x版本升级Mysql ------------------#! /bin/sh #1.关闭selinuxcp -rp /e ...

  4. dataGridView 如何默认选中第一行

    datagridview默认选中第一行方法: this.dataGridView1.Rows[0].Selected = true; datagridview 去除 默认选中第一行方法:在绑定data ...

  5. C#关于值类型和引用类型的备忘

      值类型 引用类型 内存分配地点 分配在栈中 分配在堆中 效率 效率高,不需要地址转换 效率低,需要进行地址转换 内存回收 使用完后,立即回收 使用完后,不是立即回收,等待GC回收 赋值操作 进行复 ...

  6. php 执行事务的时候pdo出现问题

    新版本的pdo会有这个问题: General error: 2014 Cannot execute queries while other unbuffered queries are active. ...

  7. php生成随机字符串和验证码的类

    网上有很多的php随机数与验证码的代码与文章,真正适用的没有几个. 索性自己搞一个吧. 开始本节的php教程 吧,以下代码的实现,主要做到可以很好区分一个get_code(),另一个create_ch ...

  8. activity切换动画特效

    效果图: 结构图: 测试代码: 布局: <?xml version="1.0" encoding="utf-8"?> <LinearLayou ...

  9. IOS学习3

    @property属性使用 copy:NSString strong: 一般对象 weak: UI空间 assign:基本数据类型 retain: (对象,先上述类型使用) id 万能指针. id缺点 ...

  10. DevExpress LookUpEdit和ComboBoxEdit部分用法

    LookUpEdit 1.绑定列 (注意点:LookUpEdit1的FieldName要和绑定的列明一致) 方式一: LookUpEdit1.Properties.DisplayMember = &q ...