当前位置:网站首页>LeetCode 61. Rotating linked list
LeetCode 61. Rotating linked list
2022-08-10 12:00:00 【Mizuna pen】
原题网址:https://leetcode.cn/problems/rotate-list/
Give a linked list head node,给一个值k,Let the linked list rotate clockwisek次;
1-2-3-4;k=2
3-4-1-2;
In fact, it is to find the last little bit.俩表长度-k%链表长度
public ListNode rotateRight(ListNode head, int k) {
if(head == null) {
return head;
}
int size = 0;
ListNode node = head;
ListNode tail = null;
// Find the trailing node and length
while(node != null) {
size++;
if(node.next == null) {
tail = node;
}
node = node.next;
}
tail.next = head;
k = k%size;
node = head;
// 要转k次.I was looking for it beforesize-k-1的节点.
int tmp = size-k-1;
while(tmp>0) {
node = node.next;
tmp--;
}
ListNode next = node.next;
node.next = null;
return next;
}
边栏推荐
- AutoCAD Map 3D功能之一暴力处理悬挂点(延伸)
- 态路小课堂丨如何为CXP光模块选择光纤跳线?
- Codeforces 862 C. Mahmoud and Ehab and the xor (技巧)
- HDU 4135:Co-prime (容斥原理)
- 面试官:你们是如何保证接口的幂等性?
- OPNsense安装配置Zenarmor
- Nocalhost - Making development more efficient in the cloud-native era
- LeetCode50天刷题计划(Day 16—— 两两交换链表中的节点(9.10-10.30)
- Licking Exercise - 63 Find all anagrams in a string
- 力扣练习——61 根据字符出现频率排序
猜你喜欢
dedecms支持Word内容一键导入
推荐6个自媒体领域,轻松易上手
A little self-deprecating deconstruction about farmers "code"
rider内Mono脚本找不到引用资源
Database management tool: dynamic read-write separation
LeetCode50天刷题计划(Day 19—— 在排序数组中查找元素的第一个和最后一个位置(9.10-10.40)
CLIP还能做分割任务?哥廷根大学提出一个使用文本和图像prompt,能同时作三个分割任务的模型CLIPSeg,榨干CLIP能力...
VSCode远程连接服务器报错:Could not establish connection to “xxxxxx”的可能错误原因及解决
建校仅11年就入选“双一流” ,这所高校是凭什么做到的?
[E-commerce operation] Do you really understand social media marketing (SMM)?
随机推荐
Buckle Exercise - 61 Sort by frequency of characters
leetcode 823. Binary Trees With Factors(因子二叉树)
一文读懂NFT数字藏品为何风靡全球?
Cannot find symbol log because lombok is not found
托米的咒语
快手“弃”有赞与微盟“结亲”,电商SaaS行业竞争格局将变?
LeetCode 445. 两数相加 II
Ssm framework construction process [easy to understand]
不止跑路,拯救误操作rm -rf /*的小伙儿
An enhanced dynamic packet buffer management.论文核心部分
Redis常用命令
LeetCode 92. 反转链表 II
因为找不到lombok而找不到符号log
微信小程序提交审核历史版本记录从哪里查看
gpu-admission 源码分析
VSCode remote connection server error: Could not establish connection to "xxxxxx" possible error reasons and solutions
StoneDB Document Bug Hunting Season 1
HDU 6040 Hints of sd0061 (技巧)
Go 事,Gopher 要学的数字类型,变量,常量,运算符 ,第2篇
LeetCode50天刷题计划(Day 17—— 下一个序列(14.50-16.30)