力扣算法题—147Insertion_Sort_List

Sort a linked list using insertion sort.


A graphical example of insertion sort. The partial sorted list (black) initially contains only the first element in the list.
With each iteration one element (red) is removed from the input data and inserted in-place into the sorted list

Algorithm of Insertion Sort:

  1. Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list.
  2. At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there.
  3. It repeats until no input elements remain.

Example 1:

Input: 4->2->1->3
Output: 1->2->3->4

Example 2:

Input: -1->5->3->4->0
Output: -1->0->3->4->5

Solution:  就是简单的插入算法
 1 class Solution {
 2 public:
 3     ListNode *insertionSortList(ListNode *head) {
 4         if (head == nullptr || head->next == nullptr)return head;
 5         ListNode *durry, *p, *pre, *cur, *next;
 6         durry = new ListNode(-1);
 7         durry->next = head;
 8         p = pre = cur = next = head;
 9         next = cur->next;
10         while (next != nullptr)
11         {
12             cur = next;
13             next = cur->next;
14             p = durry;
15             while (p != cur)
16             {
17                 if (p->next->val > cur->val)
18                 {
19                     pre->next = next;
20                     cur->next = p->next;
21                     p->next = cur;
22                     break;
23                 }
24                 p = p->next;
25             }
26             if (pre->next == cur)//未移动过
27                 pre = cur;
28         }
29         return durry->next;
30     }
31 };

原文地址:https://www.cnblogs.com/zzw1024/p/11768749.html

时间: 2024-08-30 14:52:30

力扣算法题—147Insertion_Sort_List的相关文章

力扣算法题—042接雨水

1 #include"000库函数.h" 2 //一点头绪都没有 3 //然后就自己按自己的意思来一遍 4 //好像没有用算法 5 //16ms,让我激动一把 6 7 class Solution { 8 public: 9 int trap(vector<int>& height) { 10 if (height.size() < 2)return 0; 11 int s = 0;//起始点 12 int e = 0;//终止点 13 int v = 0;/

力扣算法题—050计算pow(x, n)

1 #include "000库函数.h" 2 3 4 5 //使用折半算法 牛逼算法 6 class Solution { 7 public: 8 double myPow(double x, int n) { 9 if (n == 0)return 1; 10 double res = 1.0; 11 for (int i = n; i != 0; i /= 2) { 12 if (i % 2 != 0) 13 res *= x; 14 x *= x; 15 } 16 return

力扣算法题—064最小路径之和

给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. 说明:每次只能向下或者向右移动一步. 示例: 输入: [   [1,3,1], [1,5,1], [4,2,1] ] 输出: 7 解释: 因为路径 1→3→1→1→1 的总和最小. 1 #include "_000库函数.h" 2 3 //使用Dijkstra算法 4 //即动态规划 5 class Solution { 6 public: 7 int minPathSum(vec

力扣算法题—048旋转图像

1 #include "000库函数.h" 2 3 //找位置规律 4 //先不按照规则,使用另一个矩阵 5 class Solution { 6 public: 7 void rotate(vector<vector<int>>& matrix) { 8 vector < vector<int>>v = matrix; 9 int n = matrix.size(); 10 for (int t = 0; t < 1; +

力扣算法题—062不同路径

一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为“Finish”). 问总共有多少条不同的路径? 例如,上图是一个7 x 3 的网格.有多少可能的路径? 说明:m 和 n 的值均不超过 100. 示例 1: 输入: m = 3, n = 2 输出: 3 解释: 从左上角开始,总共有 3 条路径可以到达右下角. 1. 向右 -> 向右 -> 向下 2. 向右 -> 向下

力扣算法题—072编辑距离

给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 . 你可以对一个单词进行如下三种操作: 插入一个字符 删除一个字符 替换一个字符 示例 1: 输入: word1 = "horse", word2 = "ros" 输出: 3 解释: horse -> rorse (将 'h' 替换为 'r') rorse -> rose (删除 'r') rose -> ros (删除 'e') 示例 2: 输

力扣算法题—078集合

给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: nums = [1,2,3] 输出: [ [3],   [1],   [2],   [1,2,3],   [1,3],   [2,3],   [1,2],   [] ] 1 #include "_000库函数.h" 2 3 4 //这道题就是求不同的子集问题 5 class solution { 6 public: 7 vector<vector<i

力扣算法题—085最大矩阵

给定一个仅包含 0 和 1 的二维二进制矩阵,找出只包含 1 的最大矩形,并返回其面积. 示例: 输入: [ ["1","0","1","0","0"], ["1","0","1","1","1"], ["1","1","1","1&quo

力扣算法题—092反转链表2

反转从位置 m 到 n 的链表.请使用一趟扫描完成反转. 说明:1 ≤ m ≤ n ≤ 链表长度. 示例: 输入: 1->2->3->4->5->NULL, m = 2, n = 4 输出: 1->4->3->2->5->NULL 1 #include "_000库函数.h" 2 3 4 struct ListNode { 5 int val; 6 ListNode *next; 7 ListNode(int x) : val