Leetcode#147 Insertion Sort List
心得:有关链表的题目,多用中间变量,代码写得清晰一点,适当注释
代码:
ListNode *insertionSortList(ListNode *head) {
if (!head) return NULL;
ListNode *curr = head->next;
ListNode *prev = head;
while (curr) {
ListNode *p = NULL;
ListNode *c = head;
// 寻找插入位置
while (c->val < curr->val) {
p = c;
c = c->next;
}
// 如果要插入的位置就是当前位置
if (c == curr) {
prev = curr;
curr = curr->next;
}
// 否则插入节点
else {
prev->next = curr->next;
curr->next = c;
if (!p) // 如果带插入的位置是队首
head = curr;
else // 普通情况
p->next = curr;
curr = prev->next;
}
}
return head;
}
Leetcode#147 Insertion Sort List的更多相关文章
- [LeetCode] 147. Insertion Sort List 链表插入排序
Sort a linked list using insertion sort. A graphical example of insertion sort. The partial sorted l ...
- Java for LeetCode 147 Insertion Sort List
Sort a linked list using insertion sort. 解题思路: 插入排序,JAVA实现如下: public ListNode insertionSortList(List ...
- leetcode 147. Insertion Sort List ----- java
Sort a linked list using insertion sort. 插入排序. /** * Definition for singly-linked list. * public cla ...
- [LeetCode] 147. Insertion Sort List 解题思路
Sort a linked list using insertion sort. 问题:实现单向链表的插入排序. 这是比较常规的一个算法题目. 从左往右扫列表,每次将指针的下一个元素插入前面已排好序的 ...
- LeetCode 147. Insertion Sort List 链表插入排序 C++/Java
Sort a linked list using insertion sort. A graphical example of insertion sort. The partial sorted l ...
- [leetcode] 147. Insertion Sort List (Medium)
原题 别人的思路 非常简洁 function ListNode(val) { this.val = val; this.next = null; } /** * @param {ListNode} h ...
- [LeetCode]147. Insertion Sort List链表排序
插入排序的基本思想 把排好的放在一个新的变量中,每次拿出新的,排进去 这个新的变量要有超前节点,因为第一个节点可能会有变动 public ListNode insertionSortList(List ...
- 【LeetCode】147. Insertion Sort List 解题报告(Python)
[LeetCode]147. Insertion Sort List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
- LeetCode OJ 147. Insertion Sort List
Sort a linked list using insertion sort. Subscribe to see which companies asked this question 解答 对于链 ...
随机推荐
- php输出utf-8格式
header("Content-type:text/html;charset=utf-8"); 输出数据前插入以上代码,以utf-8格式输出,避免乱码
- 【Window】Tor(洋葱头路由)+Privoxy 网络实践(附带Java实例代码)
1.背景 平时我们需要访问onion后缀的网站,需要通过Tor(The Onion Router,洋葱路由器).一般来说安装Tor Broswer就可以满足需要.但是项目我要做的是通过程序来获取oni ...
- Delphi For Android 开发笔记-附:如何Delphi中同时实现Windows、Android版的GetModuleFileName函数
在Windows中开发DLL时,经常会需要获取当前DLL所在目录以便读取同目录下的其他文件,而目前Delphi在开发android时,其实没多大必要获取,因为整个工程只有一个so文件,而这个so文件也 ...
- 【转】资源文件在Delphi编程中的应用
段东宁 计亚南 (郴州职业技术学院, 湖南 郴州 423000) 摘要: 资源文件是一种能有效地组织.管理和使用资源的文件形式,在软件开发中有着广泛的应用.本文详细介绍了在Delphi编程中资源文件 ...
- DevExpress 中 WaitForm 使用
第一步: 在程序中拖入: splashScreenManager1 控件 在需要处理的地方 使用以下语句来打开 WaitForm窗体(当然需要在 splashScreenManager1控件中绑定一个 ...
- p ython笔记第三天
#!/usr/bin/env python# -*- coding:utf-8 -*- # v = "李杰"# for item in v:# print(item)####### ...
- openSUSE13.1无法打开Yast的安装/移除软件管理软件的解决办法·(未解决,临时方法) 收获:有问题,读日志
看了日志发现错误在于Ruby,新的YaST是基于Ruby的,而我用rvm安了新版本Ruby,日志里这么出错: 2014-08-22 20:20:57 <3> linux-vfpp.site ...
- [笔记]--Ubuntu安装Sublime Text 2
sublime text 2 有两种安装方式,一种是添加软件源,然后用命令安装.另外一种是下载安装包.解压手动安装.Sublime Text 2 入门及技巧 一.下载安装 1.在Sublime Tex ...
- DB2中ixf文件的导入导出
1. 导出数据 语法:EXPORT TO <文件路径>/文件名.IXF OF IXF SELECT * FROM 表名 2. 导入数据 语法:db2 IMPORT FROM <路 ...
- poj 2777 Count Color
题目连接 http://poj.org/problem?id=2777 Count Color Description Chosen Problem Solving and Program desig ...