当前位置: 首页 > article >正文

leetcode 148. 排序链表

题目如下
在这里插入图片描述

数据范围

在这里插入图片描述

通过代码

/**
 * 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:
    ListNode* sortList(ListNode* head) {
        int n = 0;
        ListNode *t = head;
        vector<int> a;
        while(t != nullptr){
            n++;
            a.push_back(t->val);
            t = t -> next;
        }
        sort(a.begin(),a.end());
        t = head;
        for(int i = 0;i < a.size();i++){
            t -> val = a[i];
            t = t -> next;
        }
       
        return head;
    }
};

在这里插入图片描述


http://www.kler.cn/a/570934.html

相关文章:

  • 网络编程相关概念
  • VUE集成Live2d
  • Python爬虫实战:1688商品详情API接口指南(附代码)
  • C#中的字典怎么使用?
  • etcd部署硬件资源推荐
  • Linux下测试Wifi性能——2.Linux下wifi指令
  • 本地大模型搭建与webui交互
  • 深入理解 Vue 中的 `ref`
  • Spring Boot的启动流程
  • [算法]——位运算(三)
  • Android SDK封装与发布实战指南
  • 初学STM32之简单认识IO口配置(学习笔记)
  • c语言笔记 数组篇
  • 【C语言】联合体 `union` 的妙用
  • Linux的进程观:简单性如何成就强大性(三)
  • Unity3D Cinemachine 高级应用详解
  • Unity插件-Mirror使用方法(一)Mirror介绍
  • nuxt常用组件库html-validator、@nuxtjs/i18n、@nuxt/image、@unocss/nuxt使用解析
  • 【C#】winform设计一个等待窗口
  • 集群、分布式与微服务架构 区别