当前位置:网站首页>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;
}
边栏推荐
- How to join We Media, learn about these 5 monetization modes, and make your account quickly monetize
- 10 个 Reduce 常用“奇技淫巧”
- 电脑怎么设置屏幕息屏时间(日常使用分享)
- Module 9 - Designing an e-commerce seckill system
- LeetCode 25. A set of K flipped linked lists
- Database management tool: dynamic read-write separation
- Since the media hot style title how to write?Taught you how to write the title
- LeetCode 24. Swap nodes in linked list pairwise
- 从源码角度分析UUID的实现原理
- 个推数据资产管理经验 | 教你打造数据质量心电图,智能检测数据“心跳”异常
猜你喜欢

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

StoneDB 文档捉虫活动第一季

LAXCUS分布式操作系统安全管理

A little self-deprecating deconstruction about farmers "code"

Network sockets (UDP and TCP programming)

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

Do self-media monthly income tens of thousands?Several self-media tools that bloggers are using

即时零售业态下如何实现自动做账?

Go 事,Gopher 要学的数字类型,变量,常量,运算符 ,第2篇

3款不同类型的自媒体免费工具,有效提高创作、运营效率
随机推荐
LeetCode 362. Design Hit Counter(计数器)
LeetCode50天刷题计划(Day 19—— 在排序数组中查找元素的第一个和最后一个位置(9.10-10.40)
Buckle Exercise - 61 Sort by frequency of characters
Licking Exercise - 59 From Binary Search Trees to Greater Sum Trees
再有人问你分布式事务,把这篇扔给他
Network Fundamentals (Section 1)
[Go WebSocket] 多房间的聊天室(一)思考篇
std::move()
Since the media hot style title how to write?Taught you how to write the title
嘉为蓝鲸荣获工信部“数字技术融合创新应用解决方案”
力扣练习—— 矩形区域不超过 K 的最大数值和(hard)
Cannot find symbol log because lombok is not found
LeetCode 86. Delimited Linked List
面试官:项目中 Dao、Service、Controller、Util、Model 怎么划分的?
2016,还是到了最后
推荐6个自媒体领域,轻松易上手
【Redis】内存回收策略
基于UiAutomator2+PageObject模式开展APP自动化测试实战
16、Pytorch Lightning入门
AutoCAD Map 3D功能之一暴力处理悬挂点(延伸)