Leetcode 148. 排序链表(DAY 86) ---- Leetcode Hot 100
发布日期:2021-06-30 22:29:25 浏览次数:2 分类:技术文章

本文共 878 字,大约阅读时间需要 2 分钟。

原题题目

在这里插入图片描述


代码实现(首刷自解)

/** * Definition for singly-linked list. * struct ListNode { *     int val; *     ListNode *next; *     ListNode() : val(0), next(nullptr) {} *     ListNode(int x) : val(x), next(nullptr) {} *     ListNode(int x, ListNode *next) : val(x), next(next) {} * }; */class Solution {
public: static bool cmp(const ListNode* a,const ListNode* b) {
return a->val < b->val; } ListNode* sortList(ListNode* head) {
if(!head) return nullptr; vector
unsorted_vector; auto temp = head; while(temp) {
unsorted_vector.push_back(temp); temp = temp->next; } stable_sort(unsorted_vector.begin(),unsorted_vector.end(),cmp); unsorted_vector.push_back(nullptr); for(int i=0;i
next = unsorted_vector[i+1]; return unsorted_vector[0]; }};

转载地址:https://love6.blog.csdn.net/article/details/115736930 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Leetcode 739. 每日温度(DAY 86)---- Leetcode Hot 100
下一篇:Leetcode 64. 最小路径和(DAY 86) ---- Leetcode Hot 100

发表评论

最新留言

表示我来过!
[***.240.166.169]2024年05月03日 01时37分33秒