LeetCode | 147. Insertion Sort List
題目:
Sort a linked list using insertion sort.
A graphical example of insertion sort. The partial sorted list (black) initially contains only the first element in the list.
With each iteration one element (red) is removed from the input data and inserted in-place into the sorted list
Algorithm of Insertion Sort:
- Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list.
- At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there.
- It repeats until no input elements remain.
Example 1:
Input: 4->2->1->3 Output: 1->2->3->4
Example 2:
Input: -1->5->3->4->0 Output: -1->0->3->4->5
程式碼:
/**
* 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 || head->next == NULL)
return head;
ListNode *new_head = NULL, *p = head, *cur, *q;
while(p != NULL)
{
cur = p;
p = p->next;
if(new_head == NULL)
{
new_head = cur;
cur->next = NULL;
}
else
{
ListNode *pre = NULL;
for(q = new_head; q != NULL; q = q->next)
{
if(cur->val < q->val)
{
if(pre == NULL)
{
cur->next = q;
new_head = cur;
}
else
{
pre->next = cur;
cur->next = q;
}
break;
}
pre = q;
}
if(q == NULL)
{
pre->next = cur;
cur->next = NULL;
}
}
}
return new_head;
}
};
題外話:
轉眼又到了月底。日子一天天的翻過,如白駒過隙,時間真的好快啊。
還記得年初的時候,三月底的時候感嘆一年的四分之一就飛快過去,轉眼間今年已經過了四分之三,還有四分之一。
在家工作已經到了非常熟悉又開始質變的時候,需要做一些時間管理上的調整。
希望之後可以慢慢實行~
這個月的感慨很簡單,希望大家平平安安,健健康康就好~
願一切都好。
再見,九月;
你好,十月。
P.S. 感謝遇到了成交cp,真的又暖又甜~ 也許不管在什麼時候,美好的事物都是那麼美好吧~ 加油呀~
相關文章
- Insertion Sort and Merge Sort
- LeetCode | 148. Sort ListLeetCode
- [LeetCode] 148. Sort ListLeetCode
- 插入排序(Insertion Sort)排序
- 排序演算法(3)插入排序(Insertion Sort)排序演算法
- 排序演算法之「插入排序(Insertion Sort)」排序演算法
- 2.插入排序演算法(Insertion_Sort)排序演算法
- Leetcode Sort ColorsLeetCode
- Leetcode Sort ArrayLeetCode
- [LeetCode] 280. Wiggle SortLeetCode
- List排序Collections.sort 重寫compare排序
- [LeetCode] 451. Sort Characters By FrequencyLeetCode
- LeetCode之Sort Array By Parity(Kotlin)LeetCodeKotlin
- Mac文字排序編輯工具:Magic Sort ListMac排序
- python用List的內建函式list.sort進行排序Python函式排序
- LeetCode | 141 linked list cycleLeetCode
- Leetcode 61. Rotate ListLeetCode
- [LeetCode] 61. Rotate ListLeetCode
- Java面試-List中的sort詳細解讀Java面試
- Leetcode 206. Reverse Linked ListLeetCode
- [leetcode]linked-list-cycle-iiLeetCode
- LeetCode 382 Linked List Random NodeLeetCoderandom
- Leetcode 234. Palindrome Linked ListLeetCode
- 75. Sort Colors(Leetcode每日一題-2020.10.07)LeetCode每日一題
- [資料結構與演算法]-排序演算法之插入排序(insertion sort)及其實現(Java)資料結構演算法排序Java
- LeetCode 452. Minimum Number of Arrows to Burst Balloons Sort/MediumLeetCode
- Leetcode 142. Linked List Cycle IILeetCode
- LeetCode - Easy - 206. Reverse Linked ListLeetCode
- Leetcode 203. Remove Linked List ElementsLeetCodeREM
- Leetcode 237. Delete Node in a Linked ListLeetCodedelete
- LeetCode 138. Copy List with Random PointerLeetCoderandom
- LeetCode | 203. Remove Linked List ElementsLeetCodeREM
- LeetCode之Odd Even Linked List(Kotlin)LeetCodeKotlin
- [LeetCode] 328. Odd Even Linked ListLeetCode
- [LeetCode] 2487. Remove Nodes From Linked ListLeetCodeREM
- LeetCode 83. Remove Duplicates from Sorted ListLeetCodeREM
- [LeetCode] 430. Flatten a Multilevel Doubly Linked ListLeetCode
- Leetcode 19 Remove Nth Node From End of ListLeetCodeREM