当前位置: 首页 > news >正文

147. Insertion Sort List

Sort a linked list using insertion sort.

解法:

Well, life gets difficult pretty soon whenever the same operation on array is transferred to linked list.

First, a quick recap of insertion sort:

Start from the second element (simply a[1] in array and the annoying head -> next -> val in linked list), each time when we see a node with val smaller than its previous node, we scan from the head and find the position that the current node should be inserted. Since a node may be inserted before head, we create a new_head that points to head. The insertion operation, however, is a little easier for linked list.

Now comes the code:

/**
 * 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||!head->next) return head;
        ListNode* new_head ;
        new_head->next = head;
        ListNode* pre = new_head;
        ListNode* cur = head;
        while(cur) {
            if(cur->next && cur->next->val < cur->val) {
                while(pre->next && pre->next->val < cur->next->val) pre = pre->next;
                ListNode* temp = pre->next;
                pre->next = cur->next;
                cur->next = cur->next->next;
                pre->next->next = temp;
                pre = new_head;
            }
            else cur = cur->next;
        }
        return new_head->next;
    }
};

方法二:

class Solution {
public:
    /**
     * @param head: The first node of linked list.
     * @return: The head of linked list.
     */
    ListNode *insertionSortList(ListNode *head) {
        ListNode *dummy = new ListNode(0);
        // 这个dummy的作用是,把head开头的链表一个个的插入到dummy开头的链表里
        // 所以这里不需要dummy->next = head;

        while (head != NULL) {
            ListNode *temp = dummy;
            ListNode *next = head->next;
            while (temp->next != NULL && temp->next->val < head->val) {
                temp = temp->next;
            }
            head->next = temp->next;
            temp->next = head;
            head = next;
        }

        return dummy->next;
    }
};

转载于:https://www.cnblogs.com/CarryPotMan/p/5343689.html

相关文章:

  • java值类型和引用类型
  • 如何查看oracle表空间是否自动扩展
  • UBuntu14.04下安装和卸载Qt5.3.1
  • LeetCode 74 Search a 2D Matrix(搜索2D矩阵)
  • CentOS 6安装配置LDAP
  • 习题6-2 S-Trees(树)
  • centos6.x 抓取ssh登录的用户名和密码
  • Win7域用户实现User权限安装共享打印机
  • 用 gitbook 为项目写本书吧
  • WinCE6.0多国语言软键盘
  • Codeforces Round #344 (Div. 2) E. Product Sum 维护凸壳
  • 20145237《Java程序设计》第一周学习总结
  • ListView之SimpleAdapter
  • HashMap的工作原理及HashMap和Hashtable的区别
  • 多人聊天
  • 【跃迁之路】【477天】刻意练习系列236(2018.05.28)
  • 【跃迁之路】【519天】程序员高效学习方法论探索系列(实验阶段276-2018.07.09)...
  • Android开源项目规范总结
  • Angular数据绑定机制
  • bootstrap创建登录注册页面
  • exif信息对照
  • Hibernate最全面试题
  • PHP 7 修改了什么呢 -- 2
  • PHP面试之三:MySQL数据库
  • PV统计优化设计
  • Python学习笔记 字符串拼接
  • SAP云平台运行环境Cloud Foundry和Neo的区别
  • vuex 笔记整理
  • 搞机器学习要哪些技能
  • 好的网址,关于.net 4.0 ,vs 2010
  • 入口文件开始,分析Vue源码实现
  • 什么软件可以剪辑音乐?
  • 实战:基于Spring Boot快速开发RESTful风格API接口
  • 一加3T解锁OEM、刷入TWRP、第三方ROM以及ROOT
  • 正则与JS中的正则
  • Python 之网络式编程
  • #android不同版本废弃api,新api。
  • #NOIP 2014# day.2 T2 寻找道路
  • #QT(TCP网络编程-服务端)
  • (3)nginx 配置(nginx.conf)
  • (Note)C++中的继承方式
  • (pojstep1.1.2)2654(直叙式模拟)
  • (SpringBoot)第七章:SpringBoot日志文件
  • (zz)子曾经曰过:先有司,赦小过,举贤才
  • (二十五)admin-boot项目之集成消息队列Rabbitmq
  • (力扣记录)1448. 统计二叉树中好节点的数目
  • (六) ES6 新特性 —— 迭代器(iterator)
  • (南京观海微电子)——COF介绍
  • (十)DDRC架构组成、效率Efficiency及功能实现
  • (十三)Maven插件解析运行机制
  • (小白学Java)Java简介和基本配置
  • (一)Dubbo快速入门、介绍、使用
  • (一)Java算法:二分查找
  • .bat批处理(二):%0 %1——给批处理脚本传递参数
  • .NET Core MongoDB数据仓储和工作单元模式封装