本文主要是介绍*Leetcode 147. Insertion Sort List,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
https://leetcode.com/problems/insertion-sort-list/description/
理清楚插入排序的思路,就能1A
class Solution {
public:ListNode* insertSort(ListNode* head) {if (!head) return NULL;ListNode *cur = head, *pre = NULL, *min_pre = NULL, *min_pos = head;while (cur) {if (cur->val < min_pos->val) {min_pre = pre;min_pos = cur;}pre = cur;cur = cur->next;}if (min_pre == NULL) {return head;} else {min_pre->next = min_pos->next;min_pos->next = head;}return min_pos;}ListNode* insertionSortList(ListNode* head) {if (!head) return NULL;ListNode* cur = insertSort(head);ListNode* new_head = cur;while(cur->next) {cur->next = insertSort(cur->next);cur = cur -> next;}return new_head;}
};
这篇关于*Leetcode 147. Insertion Sort List的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!