Insertion Sort List问题及解法
来源:互联网 发布:词典推荐 知乎 魏 编辑:程序博客网 时间:2024/05/24 06:50
问题描述:
Sort a linked list using insertion sort.
问题分析:
对链表进行插入排序。
过程详见代码:
/** * 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 pre(head->val);pre.next = head;ListNode * ptr = head->next, *tail = head;tail->next = NULL;while (ptr){ListNode * temp = ptr->next;ptr->next = NULL;if (ptr->val >= pre.val){pre.val = ptr->val;tail->next = ptr; tail = ptr;}else{head = pre.next;ListNode *last = ⪯while (head){if (head->val >= ptr->val){last->next = ptr;ptr->next = head; break;}last = head;head = head->next;}}ptr = temp;}return pre.next; }};
阅读全文
0 0
- Insertion Sort List问题及解法
- Sort List问题及解法
- leetcode之147. Insertion Sort List(C++解法)
- Sort Colors问题及解法
- Insertion Sort List | leetcode
- Leetcode: Insertion Sort List
- [LeetCode] Insertion Sort List
- Insertion Sort List
- Leetcode: Insertion Sort List
- [LeeCode] Insertion Sort List
- leetcode-Insertion Sort List
- [LeetCode]Insertion Sort List
- Insertion Sort List
- 【LeetCode】Insertion Sort List
- LeetCode | Insertion Sort List
- LeetCode - Insertion Sort List
- Insertion Sort List
- Insertion Sort List
- (五)展示界面数据
- 最长递增子序列
- java中list、set和map 的区别
- 使用Node.js+Socket.IO搭建WebSocket实时应用
- Android5.0特性
- Insertion Sort List问题及解法
- ios cookie概念介绍
- VS2013 编写汇编程序
- Ultra-QuickSort(逆序数)
- [cs231n之第四课]Backprop
- Spring IoC有什么好处呢
- 贪心算法——招聘会
- vue2.0入门教程-笔记1
- 40编程练习