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的更多相关文章

  1. LeetCode OJ 147. Insertion Sort List

    Sort a linked list using insertion sort. Subscribe to see which companies asked this question 解答 对于链 ...

  2. 【LeetCode OJ】Interleaving String

    Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...

  3. 【LeetCode OJ】Reverse Words in a String

    Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...

  4. LeetCode OJ:Insertion Sort List (插入排序链表)

    Sort a linked list using insertion sort. 用插入排序来排序一个list,额, 我写的好麻烦啊, debug了好久,至少提交了5次...写吐了快,先贴代码,写的也 ...

  5. 【LEETCODE OJ】Reorder List

    Problem link: http://oj.leetcode.com/problems/reorder-list/ I think this problem should be a difficu ...

  6. 【LeetCode OJ】Validate Binary Search Tree

    Problem Link: https://oj.leetcode.com/problems/validate-binary-search-tree/ We inorder-traverse the ...

  7. 【LeetCode OJ】Recover Binary Search Tree

    Problem Link: https://oj.leetcode.com/problems/recover-binary-search-tree/ We know that the inorder ...

  8. 【LeetCode OJ】Same Tree

    Problem Link: https://oj.leetcode.com/problems/same-tree/ The following recursive version is accepte ...

  9. 【LeetCode OJ】Symmetric Tree

    Problem Link: https://oj.leetcode.com/problems/symmetric-tree/ To solve the problem, we can traverse ...

随机推荐

  1. [转载]使用SoapUI进行负载测试

    使用了SoapUI进行负载测试 http://www.cnblogs.com/zerotest/p/4668918.html 负载测试是相当独特的,我们已经创建了一个功能,使您能够快速创建性能测试,并 ...

  2. cf(#div1 A. Dreamoon and Sums)(数论)

    A. Dreamoon and Sums time limit per test 1.5 seconds memory limit per test 256 megabytes input stand ...

  3. hdu----(5047)Sawtooth(大数相乘+数学推导)

    Sawtooth Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Su ...

  4. hdu-----(1151)Air Raid(最小覆盖路径)

    Air Raid Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  5. 你不知道的JavaScript-- 事件流与事件处理

    转载:http://blog.csdn.net/i10630226/article/details/48970971 1. 事件处理 1.1. 绑定事件方式 (1)行内绑定 语法: //最常用的使用方 ...

  6. iOS开发中你是否遇到这些经验问题

    前言 小伙伴们在开发中难免会遇到问题, 你是如何解决问题的?不妨也分享给大家!如果此文章其中的任何一条问题对大家有帮助,那么它的存在是有意义的! 反正不管怎样遇到问题就要去解决问题, 在解决问题的同时 ...

  7. 聚类算法:ISODATA算法

    1. 与K-均值算法的比较 –K-均值算法通常适合于分类数目已知的聚类,而ISODATA算法则更加灵活: –从算法角度看, ISODATA算法与K-均值算法相似,聚类中心都是通过样本均值的迭代运算来决 ...

  8. 推荐10款非常优秀的 HTML5 开发工具

      HTML5 发展如火如荼,随着各大浏览器对 HTML5 技术支持的不断完善以及 HTML5 技术的不断成熟,未来 HTML5 必将改变我们创建 Web 应用程序的方式.今天这篇文章向大家推荐10款 ...

  9. mysql 忘记root密码修改方法

    先将mysql安装bin目录(例如:c:xxx\xxx\mysql\bin  加入环境变量) 1.在命令行窗口下输入net stop mysql5 或 net stop mysql 2.开一个命令行窗 ...

  10. Jquery表单提交方式

    1.使用调用submit方法 function tes1(){ //执行判断 if(校验通过){ $("#formId").submit(); }else{ return; } } ...