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

leetcode 61. Rotate List和86. Partition List

目录

61. Rotate List

 86. Partition List


61. Rotate List

代码:

/*** 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* rotateRight(ListNode* head, int k) {if(head == nullptr)return head;if(k == 0)return head;ListNode* cur = head;int count = 0;while(cur){count++;cur = cur->next;}k = k%count;if(k == 0)return head;ListNode *pre = nullptr;cur = head;for(int i = 0;i < count -k;i++){pre = cur;cur = cur->next;}if(pre ==nullptr)return head;pre->next = nullptr;ListNode* newhead = cur;while(cur->next){cur = cur->next;}cur->next = head;return newhead;}
};

 86. Partition List

代码:

/*** 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* partition(ListNode* head, int x) {if(head == nullptr)return nullptr;ListNode* left_dummy = new ListNode(-1,nullptr);ListNode* right_dummy =new ListNode(-1,nullptr);ListNode* leftpre = left_dummy;ListNode* rightpre = right_dummy;ListNode* cur = head;while(cur){if(cur->val < x){leftpre->next = cur;leftpre = cur;}else{rightpre->next = cur;rightpre = cur;}cur = cur->next;}leftpre->next = right_dummy->next;rightpre->next = nullptr;ListNode* ans = left_dummy->next;delete left_dummy;delete right_dummy;return ans;}
};

相关文章:

  • 搭建自己的语音对话系统:开源 S2S 流水线深度解析与实战
  • 实验-设计一个应用系统(计算机组成原理)
  • CentOS停止维护了,解决yum不能安装软件的问题
  • windows bat 在目录下(包括子目录)搜索批量指定文件名称复制到另一个文件夹内
  • QT聊天项目DAY12
  • Git企业级——进阶
  • 达梦数据库-学习-21-C 外部函数
  • 怎么判断一个Android APP使用了Cordova这个跨端框架
  • ubuntu设置开机不输密码笔记
  • 《STL--- vector的使用及其底层实现》
  • 会话管理有哪些
  • 【三维重建】【3DGS系列】【深度学习】3DGS的理论基础知识之3D高斯椭球
  • 【三维重建】【3DGS系列】【深度学习】3DGS的理论基础知识之协方差矩阵控制椭球
  • JavaScript篇:解密ES6的“藏宝图“:Set和Map的奇妙冒险
  • 基于注解的Sentinel限流熔断
  • Sentinel+OpenFeign实现服务熔断与降级:构建弹性微服务架构的核心实践
  • PET,Prompt Tuning,P Tuning,Lora,Qlora 大模型微调的简介
  • PyQt5安装,在Pycharm上配置以及使用教程
  • spring注解旁路问题讨论
  • Wkhtmltopdf使用
  • 深圳做兼职的网站/百度推广时间段在哪里设置
  • 网站开发功能需求清单/优化大师官方免费
  • 网站开发后端技术/百度推广点击一次多少钱
  • 建设商务网站的步骤/推广网址
  • 常用的设计网站/手机百度网页版入口
  • 在政务网站建设与管理上的讲话/整合营销传播方案案例