当前位置:网站首页>Leetcode61. 旋转链表
Leetcode61. 旋转链表
2022-07-28 22:06:00 【Java全栈研发大联盟】
题目传送地址: https://leetcode.cn/problems/rotate-list/
运行效率:
代码如下:
public static ListNode rotateRight(ListNode head, int k) {
//处理边界情况
if(head==null||head.next==null||k==0){
return head;
}
//先要知道链表的长度是多少
int length = 1;
ListNode tailNode = head; //尾结点
//遍历的过程顺便也把尾结点找到
while (tailNode.next != null) {
length++;
tailNode = tailNode.next;
}
//比如链表长度是6,当k=11和k=5旋转以后的效果是一样的
k = k % length;
if(k==0){
return head;
}
//新链表的头结点在原链表中的位置是length-k 新链表的尾结点在原链表中的位置是length-k
ListNode newTailNode = head;
int index = 0;
while (index < length - k-1) {
newTailNode = newTailNode.next;
index++;
}
ListNode newHeadNode = newTailNode.next;
newTailNode.next = null;
tailNode.next = head;
return newHeadNode;
}
边栏推荐
- 多传感器融合定位(一)——3D激光里程计
- 浪潮ClusterEngineV4.0 远程命令执行漏洞 CVE-2020-21224
- Wildcard ssl/tls certificate
- Js判断数据类型的4种⽅式
- sql 左连接,内连接 的写法「建议收藏」
- The computer doesn't know what to uninstall, can't open the calculator, can't edit screenshots, can't open txt files, and so on
- Jincang database kingbasees client Programming Interface Guide - ODBC feature support constraints
- Manufacturing steps of interactive slide screen in exhibition hall
- Worthington丨STEMxyme的分类和测定方法
- [self] - brush questions BFS
猜你喜欢

剑指 Offer 64. 求1+2+…+n,逻辑运算符短路效应

AUTOCAD——Excel表格导入CAD、CAD合并两兄弟

A new generation of ultra safe cellular battery, Sihao aipao, will be available from 139900 yuan
![[self] - brush questions BFS](/img/e9/e90557c63c217a43c6a5d9de0d0869.png)
[self] - brush questions BFS

智能垃圾桶(七)——SG90舵机的介绍与使用(树莓派pico实现)

阻塞式队列

Codeforces Round #810 (Div. 2) A - C

Mongodb index add, view, export, delete

Inspur clusterenginev4.0 remote command execution vulnerability cve-2020-21224

xss.haozi.me靶场详解
随机推荐
2022-07-28:以下go语言代码输出什么?A:AA;B:AB;C:BA;D:BB。 package main import ( “fmt“ ) func
Compatibility description between kingbasees and Oracle (4. SQL)
Zabbix 5.0 使用自带Redis模版监控
猿人学第二十题
请简述list,set,map类型的集合的各自特点(简述三种不同的继承方式下)
尿酸酶丨Worthington猪肝尿酸酶的特征:
[self] - question brushing - peak value
[data mining engineer - written examination] Dahua shares in 2022
Hutool official website (is hutool easy to use)
脲酶丨Worthington杰克豆脲酶的特性及测定方案
[detailed and super simple] how to use websocket links
2022 R2 mobile pressure vessel filling test question simulation test platform operation
(22) two permutation (DP), package delivery (greedy)
Machine learning problem notes
Leetcode60. 排列序列
从XSS Payload学习浏览器解码
Pycharm new project
齐博建站指南(艾戈勒)
Mongodb index add, view, export, delete
Leetcode 763. partition labels divide alphabetic intervals (medium)