LeetCode147—Insertion Sort List
2016-11-16 20:10
423 查看
原题
原题链接Sort a linked list using insertion sort.
Subscribe to see which companies asked this question
分析
链表的插入排序,这题偷了个懒,直接用了STL,对val进行排序,再将各个节点重新串起来。关于链表的插入排序,过段时间补充。
代码
bool comp(ListNode*x,ListNode*y) { return x->val<y->val; } class Solution { public: ListNode* insertionSortList(ListNode*head) { if(!head) return NULL; ListNode*p=head; vector<ListNode*>vec; while(p!=NULL) { vec.push_back(p); p=p->next; } sort(vec.begin(),vec.end(),comp); int i; for(i=0;i<vec.size()-1;i++) { vec[i]->next=vec[i+1]; } vec[i]->next=NULL; head=vec[0]; return head; } };
相关文章推荐
- (Leetcode 147)Insertion Sort List
- leetcode[147]Insertion Sort List
- [leetcode147]insertion sort list
- LeetCode 147 — Insertion Sort List(C++ Java Python)
- LeetCode 147 Insertion Sort List
- leetcode-147 Insertion Sort List
- LeetCode147: Insertion Sort List
- [Leetcode]#147 Insertion Sort List
- LeetCode(147) Insertion Sort List
- leetcode-Insertion Sort List-147
- Leetcode 147 Insertion Sort List
- Java for LeetCode 147 Insertion Sort List
- LeetCode 147:Insertion Sort List
- leetcode 147 Insertion Sort List java 算法
- 【LEETCODE】147- Insertion Sort List [Python]
- LeetCode 147 Insertion Sort List(链表)
- leetcode[147]:Insertion Sort List
- [LeetCode]147 Insertion Sort List
- LeetCode147_Insertion Sort List(用插入排序算法对链表进行排序) Java题解
- LeetCode 147 Insertion Sort List