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

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;}
};
http://www.xdnf.cn/news/8368.html

相关文章:

  • 【心海资源】黄金首饰价格查询单页源码
  • 如何在 Windows 11 或 10 上通过 PowerShell 安装 Docker Desktop
  • Centos7和Centos8版本功能对比
  • 系统性能分析基本概念(2):性能模型
  • ​​Resin-3.1.12-01 安装教程:详细步骤与配置指南(Linux环境)
  • 【运维实战】nginx版本升级
  • 我的世界模组开发——水平方向的方块(3)
  • 5.22本日总结
  • 人形机器人,进入第一阶段概念设计和起泡沫的阶段
  • 组合问题(分割字符串)
  • 【Java持久层框架对比与使用】
  • 【详解自定义类型:联合和枚举】:联合体类型的声明、特点、大小的计算,枚举类型的声明、优点和使用
  • 522UART是什么
  • 4. 寻找两个正序数组的中位数
  • 复盘20250522
  • C++:list容器,deque容器
  • 六大设计原则
  • 如何在 FastAPI 中合理使用 Pydantic 的 Alias
  • UE4 Simulation Stage 制作 平流
  • 开疆智能Profinet转RS485网关连接富士电机配置案例
  • 问题 | 撰写一份优秀的技术文档,既是科学也是艺术。
  • 模仿医学专家思维的Citrus:助力医疗决策支持
  • 自定义类型-联合体
  • 十进制转二进制
  • git@gitee.com: Permission denied (publickey). fatal: 无法读取远程仓库
  • N-gram语言模型原理与实战教程
  • sqli-labs第二十一/二十二关——POST-base64
  • STL 转 STP 深度技术指南:从 3D 打印模型到工程标准的跨领域转换全解析(附迪威模型在线方案)
  • 亚马逊选品可以从以下几个方面着手
  • 浙江大学python程序设计(陈春晖、翁恺、季江民)习题答案-第十章