【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 ...
随机推荐
- Redis与Java - 数据结构
Redis与Java 标签 : Java与NoSQL Redis(REmote DIctionary Server) is an open source (BSD licensed), in-memo ...
- 笔记8:winfrom连接数据库DBHelp
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- collectionView初始化
collectionView初始化时一定要加layout.不然会报错: UICollectionView must be initialized with a non-nil layout param ...
- ScrollView嵌套StackView提示需要宽度和高度限制
场景: 在一个xib的view中,添加一个ScrollView,再在这个ScrollView中添加一个StackView,StackView中不加控件(用代码动态加). 问题: 提示ScrollVie ...
- 如何获得bin/Debug目录的路径?
这个可以使用下面的方法 答案:bin目录:通过应用程序集合AppDomain.CurrentDomain.BaseDirectory:带有"\"System.Environment ...
- ruby开源项目之Octopress:像黑客一样写博客(zhuan)
ruby开源项目之Octopress:像黑客一样写博客 百度权重查询 词库网 网站监控 服务器监控 SEO监控 Swift编程语言教程 今年一直推荐的一种写作方式.markdown语法快速成文,git ...
- Query的选择器中的通配符[id^='code']或[name^='code']
1.选择器 (1)通配符: $("input[id^='code']");//id属性以code开始的所有input标签 $("input[id$='code'] ...
- php解压zip文件
<?php header("Content-type:text/html;charset=utf-8"); function get_zip_originalsize($fi ...
- Xcode6.2创建Empty Application
运行Xcode 6,创建一个Single View Application工程. 创建好后,把工程目录下的Main.storyboard和LaunchScreen.xib删除,扔进废纸篓. 打 ...
- MJPhotoBrowser 两个bug:回到小图模式时会闪动&大图太靠近底部
最近项目需要写网络的相片视频浏览的库, 没时间重写,使用了MJPhotoBrowser,里面的一些bug 和解决写在下面 1.-[MJPhotoLoadingView setProgress:]: m ...