当前位置:网站首页>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;
}
边栏推荐
- 模块九 - 设计电商秒杀系统
- Flutter气泡框实现
- 不止跑路,拯救误操作rm -rf /*的小伙儿
- 力扣练习——64 最长和谐子序列
- VSCode remote connection server error: Could not establish connection to "xxxxxx" possible error reasons and solutions
- CPU多级缓存与缓存一致性
- POJ 1026 Cipher (置换群)
- 从脚本到剪辑,影像大师亲授的后期制作秘籍
- POJ 3101 Astronomy (数学)
- Not just running away, but saving the guy who mishandled rm -rf /*
猜你喜欢

态路小课堂丨如何为CXP光模块选择光纤跳线?

ENVI 5.3软件安装包和安装教程

StoneDB Document Bug Hunting Season 1

L2 applications from a product perspective: why is it a playground?

【勇敢饭饭,不怕刷题之链表】链表倒数节点问题

Since the media hot style title how to write?Taught you how to write the title

皕杰报表在传参乱码

A case of violent parameter tuning in machine learning

MLX90640 红外热成像仪测温传感器 手机 APP 软件 RedEye 连接详细

机器学习之暴力调参案例
随机推荐
Some tips for using Unsafe
A case of violent parameter tuning in machine learning
建校仅11年就入选“双一流” ,这所高校是凭什么做到的?
力扣练习——63 找到字符串中所有字母异位词
APP automation testing practice based on UiAutomator2+PageObject mode
推荐6个自媒体领域,轻松易上手
从产品维度来看 我们为什么不能完全信任Layer2?
POJ 3101 Astronomy (数学)
十年架构五年生活-09 五年之约如期而至
OSSCore 开源解决方案介绍
VSCode remote connection server error: Could not establish connection to "xxxxxx" possible error reasons and solutions
2022年裁员潮,失业程序员何去何从?
学长告诉我,大厂MySQL都是通过SSH连接的
gpu-admission 源码分析
【勇敢饭饭,不怕刷题之链表】链表倒数节点问题
[Brave food, not afraid to write the linked list] The problem of the penultimate node of the linked list
不止跑路,拯救误操作rm -rf /*的小伙儿
A little self-deprecating deconstruction about farmers "code"
Nocalhost - Making development more efficient in the cloud-native era
力扣练习——61 根据字符出现频率排序