Sort a linked list using insertion sort.

思路:插入排序

#include <iostream>
using namespace std; struct ListNode {
int val;
ListNode *next;
ListNode(int x): val(x), next(NULL) {}
}; class Solution {
public:
ListNode *insertionSortList(ListNode *head) {
if (!head || head->next == NULL) {
return head;
} ListNode *cur, *prev, *next, *p; cur = head->next;
head->next = NULL; while (cur) {
p = head;
prev = NULL; while ((p != NULL) && (p->val < cur->val)) {
prev = p;
p = p->next;
} next = cur->next;
if (p != NULL) {
if (prev != NULL) {
cur->next = p;
prev->next = cur;
}
else {
cur->next = p;
head = cur;
}
} else {
cur->next = NULL;
prev->next = cur;
}
cur = next;
}
return head;
}
}; int main(int argc, char *argv[]) {
ListNode *p = new ListNode();
p->next = new ListNode();
p->next->next = new ListNode(-);
p->next->next->next = new ListNode(-); Solution *solution = new Solution();
p = solution->insertionSortList(p); while (p) {
cout << p->val << endl;
p = p->next;
}
return ;
}

leetcode - [5]Insertion Sort List的更多相关文章

  1. [Leetcode Week16]Insertion Sort List

    Insertion Sort List 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/insertion-sort-list/description/ ...

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

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

  3. 【leetcode】Insertion Sort List (middle)

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

  4. Java for LeetCode 147 Insertion Sort List

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

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

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

  6. leetcode:Insertion Sort List

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

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

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

  8. leetcode 名单 Insertion Sort List

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

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

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

随机推荐

  1. 递归函数 day17

    一 递归函数 n = 1 金老板 38+2 =40n = 2 alex n+2= 金老板 36+2 = 38n = 3 wusir n+2 = alex wusir 36 def age(n): #n ...

  2. python3.6.5 路径处理与规范化

    在Linux和Mac平台上,该函数会原样返回path,在windows平台上会将路径中所有字符转换为小写,并将所有斜杠转换为饭斜杠. >>> os.path.normcase('c: ...

  3. (转)Eclipse中需要查看某个类的源码,直接按住Ctrl 然后点击想要查看的类或则方法

      文章转自:http://blog.sina.com.cn/s/blog_52f623240102vpcr.html   在Eclipse中需要查看某个类的源码,直接按住Ctrl 然后点击想要查看的 ...

  4. Jenkins+svn+maven自动部署到tomcat

    jenkins所在主机配置好,jdk,maven,Tomcat 1.配置maven,jdk环境 1) 进入配置界面--->[系统管理]--->[Global Tool Configurat ...

  5. C#编译时,提示缺少NuGet包

    A--还原Nuget包前,一定要确保你配置了该项目的包源:如果你没有那就找别人要吧. 工具-选项-Nuget包管理器-程序包源 B--配置编译时自动还原缺少的nuget包: 工具-选项-Nuget包管 ...

  6. (转)silverlight应用程序中未处理的错误代码:2104 类别:InitializeError

        解决方案:第一步:默认网站--属性-----http头 第二步:点击mime类型: 第三步:点击新建: 第四步:输入扩展名以及类型: (1) 扩展名:.xaml MIME类型:applicat ...

  7. PAT 1007 素数对猜想(20)

    1007 素数对猜想(20 分) 让我们定义d​n​​为:d​n​​=p​n+1​​−p​n​​,其中p​i​​是第i个素数.显然有d​1​​=1,且对于n>1有d​n​​是偶数."素 ...

  8. log4j日志整合输出(slf4j+commonslog+log4j+jdklogger)

    log4j日志整合输出(slf4j+commonslog+log4j+jdklogger) 博客分类: 日志   J2EE项目中,经常会用到很多第三方的开源组件和软件,这些组件都使用各自的日志组件,比 ...

  9. 可以用WebRTC来做视频直播吗?

    https://www.zhihu.com/question/25497090   作者:韦易笑链接:https://www.zhihu.com/question/25497090/answer/72 ...

  10. navicat下创建和执行存储过程