Insertion Sort List 题解

原创文章,拒绝转载

题目来源:https://leetcode.com/problems/insertion-sort-list/description/


Description

Sort a linked list using insertion sort.

Solution


class Solution {
private:
void insert(ListNode* &head, int x) {
if (head == NULL) {
head = new ListNode(x);
} else {
if (x <= head -> val) {
ListNode* node = new ListNode(x);
node -> next = head;
head = node;
return;
}
ListNode* preNode = head;
ListNode* curNode = head -> next;
while (curNode != NULL) {
if (preNode -> val <= x && x <= curNode -> val) {
ListNode* node = new ListNode(x);
preNode -> next = node;
node -> next = curNode;
return;
}
preNode = curNode;
curNode = curNode -> next;
}
preNode -> next = new ListNode(x);
}
}
public:
ListNode* insertionSortList(ListNode* head) {
ListNode* curNode = head;
ListNode* resHead = NULL;
while (curNode != NULL) {
insert(resHead, curNode -> val);
curNode = curNode -> next;
}
return resHead;
}
};

解题描述

这道题考察的是插入排序,我考虑的做法是遍历原来的链表,每次将取到的数值在新的链表中找到相应的位置,然后拆开新链表,将新的节点插入进去再接上前后的节点。

[Leetcode Week16]Insertion Sort List的更多相关文章

  1. [LeetCode] 147. Insertion Sort List 链表插入排序

    Sort a linked list using insertion sort. A graphical example of insertion sort. The partial sorted l ...

  2. 【leetcode】Insertion Sort List (middle)

    Sort a linked list using insertion sort. 思路: 用插入排序对链表排序.插入排序是指每次在一个排好序的链表中插入一个新的值. 注意:把排好序的部分和未排序的部分 ...

  3. Java for LeetCode 147 Insertion Sort List

    Sort a linked list using insertion sort. 解题思路: 插入排序,JAVA实现如下: public ListNode insertionSortList(List ...

  4. leetcode 147. Insertion Sort List ----- java

    Sort a linked list using insertion sort. 插入排序. /** * Definition for singly-linked list. * public cla ...

  5. leetcode:Insertion Sort List

    Sort a linked list using insertion sort. 分析:此题要求在链表上实现插入排序. 思路:插入排序是一种O(n^2)复杂度的算法,基本想法就是每次循环找到一个元素在 ...

  6. [LeetCode] 147. Insertion Sort List 解题思路

    Sort a linked list using insertion sort. 问题:实现单向链表的插入排序. 这是比较常规的一个算法题目. 从左往右扫列表,每次将指针的下一个元素插入前面已排好序的 ...

  7. leetcode 名单 Insertion Sort List

    Insertion Sort List Total Accepted: 24444 Total Submissions: 96639My Submissions Sort a linked list ...

  8. LeetCode 147. Insertion Sort List 链表插入排序 C++/Java

    Sort a linked list using insertion sort. A graphical example of insertion sort. The partial sorted l ...

  9. leetcode - [5]Insertion Sort List

    Sort a linked list using insertion sort. 思路:插入排序 #include <iostream> using namespace std; stru ...

随机推荐

  1. foreach循环2

    <select id="test" parameterType="java.util.List" resultType="user"& ...

  2. Spring AOP 源码解析

    什么是AOP AOP(Aspect-OrientedProgramming,面向方面编程),可以说是OOP(Object-Oriented Programing,面向对象编程)的补充和完善.OOP引入 ...

  3. BZOJ 1791 岛屿(环套树+单调队列DP)

    题目实际上是求环套树森林中每个环套树的直径. 对于环套树的直径,可以先找到这个环套树上面的环.然后把环上的每一点都到达的外向树上的最远距离作为这个点的权值. 那么直径一定就是从环上的某个点开始,某个点 ...

  4. FullCalendar(日程管理控件)

    (以下是我学习FullCalendar控件时,网络上收集的一些资料) jQuery.fullCalendar官方网址: http://arshaw.com/fullcalendar/ http://a ...

  5. 【题解】HNOI2018转盘

    何学长口中所说的‘一眼题’……然而实际上出出来我大HN全省也只有一个人A…… 首先我们需要发现一个性质:我们永远可以在最后一圈去标记所有的物品.倘若我们反复转圈,那么这完全是可以省下来的.所以我们破环 ...

  6. [ZJOI2010]数字计数 数位DP

    最近在写DP,今天把最近写的都放上来好了,,, 题意:给定两个正整数a和b,求在[a,b]中的所有整数中,每个数码(digit)各出现了多少次. 首先询问的是一个区间,显然是要分别求出1 ~ r ,1 ...

  7. Android Intent Action 一览表

    String ADD_SHORTCUT_ACTION 动作:在系统中添加一个快捷方式.. "android.intent.action.ADD_SHORTCUT" String A ...

  8. BZOJ3653 & 洛谷3899:谈笑风生——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=3653 https://www.luogu.org/problemnew/show/P3899 设 ...

  9. HDU5306:Gorgeous Sequence——题解

    http://acm.hdu.edu.cn/showproblem.php?pid=5306 给一个数组,m次操作: 1:l r x,将a[i](l<=i<=r)=min(a[i],x) ...

  10. Eclipse ADT插件 匹配的sdk tools版本

    Eclipse android ADT插件最后的版本为ADT 23.0.7 (August 2015),google不再更新. 和之匹配的android tools版本为SDK Tools r24.1 ...