当前位置:网站首页>LeetCode 61. 旋转链表
LeetCode 61. 旋转链表
2022-08-10 11:09:00 【水菜笔】
原题网址:https://leetcode.cn/problems/rotate-list/
给一个链表头节点,给一个值k,让链表顺时针的转k次;
1-2-3-4;k=2
3-4-1-2;
其实就是找最后个一点点。俩表长度-k%链表长度
public ListNode rotateRight(ListNode head, int k) {
if(head == null) {
return head;
}
int size = 0;
ListNode node = head;
ListNode tail = null;
// 找尾结点和长度
while(node != null) {
size++;
if(node.next == null) {
tail = node;
}
node = node.next;
}
tail.next = head;
k = k%size;
node = head;
// 要转k次。从前找就是size-k-1的节点。
int tmp = size-k-1;
while(tmp>0) {
node = node.next;
tmp--;
}
ListNode next = node.next;
node.next = null;
return next;
}
边栏推荐
- Centos7 environment uses Mysql offline installation package to install Mysql5.7
- 基于UiAutomator2+PageObject模式开展APP自动化测试实战
- MLX90640 红外热成像仪测温传感器 手机 APP 软件 RedEye 连接详细
- 基于UiAutomator2+PageObject模式开展APP自动化测试实战
- 10 个 Reduce 常用“奇技淫巧”
- 4 of huawei offer levels, incredibly side is easing the bit in the interview ali?
- 从源码角度分析UUID的实现原理
- AUTOCAD - reducing spline curve control points, the advanced CAD practice (3)
- 不止跑路,拯救误操作rm -rf /*的小伙儿
- 第二十二章 源代码文件 REST API 参考(四)
猜你喜欢
AUTOCAD - reducing spline curve control points, the advanced CAD practice (3)
Since the media hot style title how to write?Taught you how to write the title
ViT结构详解(附pytorch代码)
How to join We Media, learn about these 5 monetization modes, and make your account quickly monetize
mysql appears: ERROR 1524 (HY000): Plugin '123' is not loaded
Open Office XML 格式里如何描述多段具有不同字体设置的段落
Flutter气泡框实现
StoneDB Document Bug Hunting Season 1
从产品角度看 L2 应用:为什么说这是一个游乐场?
【勇敢饭饭,不怕刷题之链表】有序链表的合并
随机推荐
POJ 2891 Strange Way to Express Integers (扩展欧几里得)
Article take you understand interrupt the key driver of polling mechanism
力扣练习——60 二叉搜索子树的最大键值和
【勇敢饭饭,不怕刷题之链表】链表倒数节点问题
使用.NET简单实现一个Redis的高性能克隆版(六)
gpu-admission 源码分析
什么是幂等性?四种接口幂等性方案详解!
leetcode 823. Binary Trees With Factors(因子二叉树)
关于振弦采集模块及采集仪振弦频率值准确率的问题
Module 9 - Designing an e-commerce seckill system
关于“码农”的一点自嘲解构
一文带你搞懂中断按键驱动程序之poll机制
Nocalhost - Making development more efficient in the cloud-native era
从产品角度看 L2 应用:为什么说这是一个游乐场?
Kyligence 通过 SOC 2 Type II 审计,以可信赖的企业级产品服务全球客户
Break through the dimensional barriers and let the dolls around you move on the screen!
石墨文档打开文档时快速定位到上次写的位置
Flutter气泡框实现
从源码角度分析UUID的实现原理
【LeetCode】640. 求解方程