【LeetCode OJ】Insertion Sort List
Problem:
Sort a linked list using insertion sort.
The node of the linked list is defined as:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
The insertion sorting is one of the simpleset sorting algorithms, which is of O(n^2) time in the worst case.
The key of the insertion sorting is how to keep track of the "sorted" part and "unsorted" part. For sorting an array we just need to keep track the last index of the sorted part; for sorting a linked list, it becomes much complex since you need maintain two pointers, one points to the last sorted element and the other one points to the first unsorted element. Each time, we insert the first unsorted element into the sorted linked list and update the two pointers.
The C++ code is as follows:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *insertionSortList(ListNode *head) {
if (head == NULL) return head;
ListNode *sorted_tail = head;
ListNode *unsorted_head = sorted_tail->next;
ListNode *prev = NULL;
ListNode *p = NULL;
while (unsorted_head != NULL) {
// Find the position to insert the element after the tail
p = head;
while (p->val <= unsorted_head->val && p != unsorted_head) {
prev = p;
p=p->next;
}
// Insert
if (p == unsorted_head) sorted_tail = sorted_tail->next;
else {
sorted_tail->next = unsorted_head->next;
unsorted_head->next = p;
if (p == head) head = unsorted_head;
else prev->next = unsorted_head;
}
unsorted_head = sorted_tail->next;
}
return head;
}
};
【LeetCode OJ】Insertion Sort List的更多相关文章
- LeetCode OJ 147. Insertion Sort List
Sort a linked list using insertion sort. Subscribe to see which companies asked this question 解答 对于链 ...
- 【LeetCode OJ】Interleaving String
Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...
- 【LeetCode OJ】Reverse Words in a String
Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...
- LeetCode OJ:Insertion Sort List (插入排序链表)
Sort a linked list using insertion sort. 用插入排序来排序一个list,额, 我写的好麻烦啊, debug了好久,至少提交了5次...写吐了快,先贴代码,写的也 ...
- 【LEETCODE OJ】Reorder List
Problem link: http://oj.leetcode.com/problems/reorder-list/ I think this problem should be a difficu ...
- 【LeetCode OJ】Validate Binary Search Tree
Problem Link: https://oj.leetcode.com/problems/validate-binary-search-tree/ We inorder-traverse the ...
- 【LeetCode OJ】Recover Binary Search Tree
Problem Link: https://oj.leetcode.com/problems/recover-binary-search-tree/ We know that the inorder ...
- 【LeetCode OJ】Same Tree
Problem Link: https://oj.leetcode.com/problems/same-tree/ The following recursive version is accepte ...
- 【LeetCode OJ】Symmetric Tree
Problem Link: https://oj.leetcode.com/problems/symmetric-tree/ To solve the problem, we can traverse ...
随机推荐
- 更准确的mysql全文索引
MYSQL自带的全文索引在查找数据的时候,有非常多的限制,如字符少于3个不能搜索,常用字不能搜索 但mysql 的like进行查询的时候又非常的慢 但你需要用到比较准确的查询的时候,要么不用mysql ...
- 在VS2010中,引用了同一解决方案的另一个项目的dll,却不能正常调用(转)
目前发现的原因是,dll的.net 版本比我的程序的高 dll用的.net 4 而程序用的.net 4 client profile 转载源:http://www.cnblogs.com/szyic ...
- Java 多线程——基础知识
java 多线程 目录: Java 多线程——基础知识 Java 多线程 —— synchronized关键字 java 多线程——一个定时调度的例子 java 多线程——quartz 定时调度的例子 ...
- Python输出内容的三种方式:print输出 python脚本执行 linux直接执行
1. 在linux中安装python后,在linux命令行中输入python即可切换到Python命令行下 退出python命令行的命令: 老版本:ctrl+D 新版本:quit();或exit() ...
- SQL Server数据库(作业)
create datebase zuoye2gouse zuoyegocreate table student --学生表( Sno varchar(20) not null primary key, ...
- NodeJS无所不能:细数10个令人惊讶的NodeJS开源项目
在几年的时间里,NodeJS逐渐发展成一个成熟的开发平台,吸引了许多开发者.有许多大型高流量网站都采用NodeJS进行开发,像PayPal,此外,开发人员还可以使用它来开发一些快速移动Web框架. 除 ...
- 常用的正则表达式(例如:匹配中文、匹配html)(转载)
匹配中文字符的正则表达式: [u4e00-u9fa5] 评注:匹配中文还真是个头疼的事,有了这个表达式就好办了 匹配双字节字符(包括汉字在内):[^x00-xff] 评注:可以用来计算字符串 ...
- 部分SIM卡被曝存安全漏洞:7.5亿部手机受牵连
7月22日消息,据国外媒体报道,一安全研究人员发现部分移动SIM卡所使用的加密方式存在一个安全漏洞,可能会导致手机被黑客远程控制. DES数据加密标准的SIM卡——DES是一种较旧的标准,目前正被部分 ...
- POJ 2240 && ZOJ 1082 Arbitrage 最短路,c++ stl pass g++ tle 难度:0
http://poj.org/problem?id=2240 用log化乘法为加法找正圈 c++ 110ms,g++tle #include <string> #include <m ...
- HDU 3567 Eight II 打表,康托展开,bfs,g++提交可过c++不可过 难度:3
http://acm.hdu.edu.cn/showproblem.php?pid=3567 相比Eight,似乎只是把目标状态由确定的改成不确定的,但是康托展开+曼哈顿为h值的A*和IDA*都不过, ...