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. C语言中printf直接打出2进制数是%什么?16进制是什么?

    #include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h&g ...

  2. 在Ubuntu系统下编译arcsim仿真器

    首先,用tar zxvf arcsim-0.2.1.tar.gz 将软件包解压 然后,打开里面的INSTALL文件,按照里面的步骤一步一步安装库.Ubuntu13.04下 1.BLAS sudo ap ...

  3. RT-thread main函数分析

    RT-thread系统的main函数位于startup.c文件中. /** * This function will startup RT-Thread RTOS. */ void rtthread_ ...

  4. BZOJ 1076 奖励关(状压期望DP)

    当前得分期望=(上一轮得分期望+这一轮得分)/m dp[i,j]:第i轮拿的物品方案为j的最优得分期望 如果我们正着去做,会出现从不合法状态(比如前i个根本无法达到j这种方案),所以从后向前推 如果当 ...

  5. javascript中面向对象的5种写法

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. BZOJ2395:[Balkan 2011]Timeismoney——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=2395 有n个城市(编号从0..n-1),m条公路(双向的),从中选择n-1条边,使得任意的两个城市 ...

  7. BZOJ4870:[SHOI2017]组合数问题——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=4870 https://www.luogu.org/problemnew/show/P3746 看网上 ...

  8. UOJ117:欧拉回路——题解

    http://uoj.ac/problem/117 (作为一道欧拉回路的板子题,他成功的令我学会了欧拉回路) (然而我不会背……) 就两件事: 1.无向图为欧拉图,当且仅当为连通图且所有顶点的度为偶数 ...

  9. LOJ2587:[APIO2018]铁人两项——题解

    https://loj.ac/problem/2587#submit_code (题面来自LOJ) 考试时候发觉树很可做,并且写了一个dp骗到了树的分. 苦于不会圆方树……现在回来发现这题还是很可做的 ...

  10. X day3

    题目 官方题解 T1: 一道水题 #include<iostream> #include<cstring> #include<cstdio> #include< ...