当前位置:网站首页>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;
}
边栏推荐
- SMIC CIM localization project suspended?Rising software: not shut down, changed to remote development!
- LeetCode 19. Delete the Nth last node of the linked list
- LeetCode 61. 旋转链表
- 搜索--01
- 16、Pytorch Lightning入门
- 托米的咒语
- Codeforces 862 C. Mahmoud and Ehab and the xor (技巧)
- 配置swagger
- LeetCode 362. Design Hit Counter
- codevs 2370 小机房的树 (LCA)
猜你喜欢

OPNsense安装配置Zenarmor

2016,还是到了最后
![[Go WebSocket] 多房间的聊天室(一)思考篇](/img/c9/4374a57c6a4ae02f606253a4c299e4.png)
[Go WebSocket] 多房间的聊天室(一)思考篇

基于UiAutomator2+PageObject模式开展APP自动化测试实战

Intel pushes 20220809 CPU microcode update to patch Intel-SA-00657 security vulnerability

mpf6_Time Series Data_quandl_correct kernel PCA_AIC_BIC_trend_log_return_seasonal_decompose_sARIMAx_ADFull

使用哈工大LTP测试分词并且增加自定义字典

做自媒体月入几万?博主们都在用的几个自媒体工具

rider内Mono脚本找不到引用资源

Nocalhost - Making development more efficient in the cloud-native era
随机推荐
LeetCode 109. Sorted Linked List Conversion Binary Search Tree
LeetCode50天刷题计划(Day 17—— 下一个序列(14.50-16.30)
快手“弃”有赞与微盟“结亲”,电商SaaS行业竞争格局将变?
LeetCode 369. Plus One Linked List
三星计划2023年开始在越南生产半导体零部件
LeetCode 61. 旋转链表
LeetCode50天刷题计划(Day 18—— 搜索旋转排序数组(8.50-12.00)
A little self-deprecating deconstruction about farmers "code"
Introduction to Software Architecture
3款不同类型的自媒体免费工具,有效提高创作、运营效率
暑期总结4
模块九 - 设计电商秒杀系统
HDU 6040 Hints of sd0061 (技巧)
Cannot find symbol log because lombok is not found
WeChat applet, global variables change in one place and the state in other places also changes.
Samsung plans to start producing semiconductor components in Vietnam in 2023
[E-commerce operation] Do you really understand social media marketing (SMM)?
有哪些好用的性能测试工具推荐?性能测试报告收费标准
LeetCode 362. Design Hit Counter
基于UiAutomator2+PageObject模式开展APP自动化测试实战