Leetcode155: Insertion Sort List
来源:互联网 发布:数学网络课程哪个好 编辑:程序博客网 时间:2024/05/17 17:57
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) { ListNode* p = head; ListNode* res = new ListNode(0); while(p) { ListNode *cur = p; p = p->next; insert(res, cur); } return res->next; } void insert(ListNode* result, ListNode* cur) { while(result->next) { if(cur->val < result->next->val) { cur->next = result->next; result->next = cur; return; } result = result->next; } if(!result->next) { result->next = cur; cur->next = NULL; } }};
0 0
- Leetcode155: Insertion Sort List
- 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
- Insertion Sort List
- Insertion Sort List
- [LeetCode] Insertion Sort List
- Git学习笔记四--远程仓库
- 软件架构
- 大数据时代的技术hive:hive介绍
- Linux磁盘扩容LVM
- tr命令详解
- Leetcode155: Insertion Sort List
- java8的时间处理
- makefile
- dbm和功率值的关系
- Handler Looper MessageQueue 深度总结
- 黑马程序员-OC学习日记2
- 软件的架构与模式之经典架构模式简介
- C++基础::函数、类、类型所在的头文件 && 接口的介绍
- URI、URL、URN介绍