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. codeforces 730 j.bottles

    J. Bottles time limit per test 2 seconds memory limit per test 512 megabytes input standard input ou ...

  2. 【bzoj2100】[Usaco2010 Dec]Apple Delivery 最短路

    题目描述 Bessie has two crisp red apples to deliver to two of her friends in the herd. Of course, she tr ...

  3. 【bzoj1334】[Baltic2008]Elect 背包dp

    题目描述 N个政党要组成一个联合内阁,每个党都有自己的席位数. 现在希望你找出一种方案,你选中的党的席位数要大于总数的一半,并且联合内阁的席位数越多越好. 对于一个联合内阁,如果某个政党退出后,其它党 ...

  4. 配置bond和vlan

    网卡是光口还是电口的方法ethtool 网卡名字 一看速度二看port是否是firber首先查看需要做bond的物理网卡,如enp130s0f0,enp131s0f0以物理网卡为enp130s0f0, ...

  5. 【刷题】BZOJ 3510 首都

    Description 在X星球上有N个国家,每个国家占据着X星球的一座城市.由于国家之间是敌对关系,所以不同国家的两个城市是不会有公路相连的. X星球上战乱频发,如果A国打败了B国,那么B国将永远从 ...

  6. Android <Android应用开发实战> 资源类型<一>

    1.字符串资源>>1.普通字符串>>2.字符串数组 <resources> <string-array name="planets_array&qu ...

  7. POJ3421:X-factor Chains——题解

    http://poj.org/problem?id=3421 题目大意:一个数列,起始为1,终止为一给定数X,满足Xi < Xi+1 并且Xi | Xi+1. 求出数列最大长度和该长度下的情况数 ...

  8. BZOJ1043:[HAOI2008]下落的圆盘——题解(配图片)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1043 Description 有n个圆盘从天而降,后面落下的可以盖住前面的.求最后形成的封闭区域的周 ...

  9. 51NOD 1149:Pi的递推式——题解

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1149 F(x) = 1 (0 <= x < 4) F(x) ...

  10. ASP.NET基础学习(暴力破解密码)

    首先写出一段登陆程序: //ashx端 <%@ WebHandler Language="C#" Class="AddCalation" %> us ...