leetcode : Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative.
For example:
Given 1->2->3->4->5->NULL
and k = 2
,
return 4->5->1->2->3->NULL
.
思路:
(1) 先求出链表的长度length
(2) k%length 防止k大于length
(3) 慢指针指向倒数第k+1,快指针指向最后一个节点
/*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/
public class Solution {private int getLength(ListNode head) {int length = 0;while(head != null) {length++;head = head.next;}return length;}public ListNode rotateRight(ListNode head, int k) {if(head == null) {return head;}ListNode dummy =new ListNode(0);dummy.next = head;ListNode fast = head;int length = getLength(head);k = k % length;while(k > 0) {fast = fast.next;k--;}while(fast.next != null) {fast = fast.next;head = head.next;}// 此时 fast 指向最后一个节点, head 指向倒数第k+1个节点fast.next = dummy.next;dummy.next = head.next;head.next = null;return dummy.next;}
}
转载于:https://www.cnblogs.com/superzhaochao/p/6479083.html
leetcode : Rotate List相关推荐
- LeetCode——Rotate Image(二维数组顺时针旋转90度)
问题: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwis ...
- LeetCode Rotate Array(数组的旋转)
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...
- LeetCode Rotate Image(矩阵的旋转)
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise ...
- [LeetCode] Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given ...
- LeetCode:Rotate Image
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- [LeetCode]Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given ...
- leetcode Rotate Array 旋转数组
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...
- leetcode Rotate Image
Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ...
- [LintCode/LeetCode] Rotate List
Problem Given a list, rotate the list to the right by k places, where k is non-negative. Example Giv ...
- LeetCode Rotate Function(寻找规律)
题意:给出一个数组a,长度为n 其中f(0)=0*a[0]+1*a[1]+...+(n-1)*a[n-1] f(1)=0*a[n-1]+1*a[0]+...+(n-1)*a[n-2] ... f(n- ...
最新文章
- 题目1154:Jungle Roads
- cv::imread导致段错误_网络诊断举例LSO导致的网络性能问题
- MFC中的模态对话框与非模态对话框
- 学生命科学要学计算机吗,现在学生物学出路真的有那么不济吗?
- httpd2.2的配置文件常见设置
- 一份 Hadoop 面试 【避坑指南】 拍了拍你!
- 用js和jq分别实现二级联动效果
- 采用SVM和神经网络的车牌识别(流程图及详细解释)
- 海美迪盒子android升级包,海美迪H5固件升级ROM系统刷机包下载_刷机教程
- Unity3D中unitypackage文件的图标显示及打开方式异常问题的解决方法(值得收藏)
- 关于hadoop运行成功但是无法链接web页面
- iVX和其它低代码平台没啥好比的 (一)
- Android——新大陆云平台篇
- U盘不被电脑识别问题
- php怎么生成一个文件夹里,php创建文件夹目录的教程
- 转载 学写钢笔字应该注意些什么
- JPA Spring Data JPA详解
- 计算机网络与多媒体试卷,《计算机网络与多媒体技术》试卷
- (三)UPF之Domain Coverage Relationship(Cover、Equivalent、Independent)
- 雨水冲刷沙堡模型——matlab元胞自动机(二)
热门文章
- AndroidDeveloper 读者专属福利
- selenium之 chromedriver与chrome版本映射表(更新至v2.46)
- hibernate--生成正向和逆向工程
- ceoi2017 Building Bridges(build)
- Linux的cat命令详解
- python模块基础之OS模块
- win7上python2.7连接mysql数据库
- C Traps:运算
- 吴昊品游戏核心算法 Round 17 —— M*N PUZZLE 与 N PUZZLE 的解的唯一性定理(由特殊到一般)...
- 动画演示 Delphi 2007 IDE 功能[6] - 快速查看 Delphi 所有的核心数据类型