LeetCode: 61. Rotate List(Medium)
1. 原题链接
https://leetcode.com/problems/rotate-list/description/
2. 题目要求
给出一个链表的第一个结点head和正整数k,然后将从右侧开始数第k个结点之后的链表与之前的链表交换位置,例如

3. 解题思路
(1)首先要注意head结点不是指头结点,而是指第一个结点;
(2)当head为null或者链表中只有一个结点时,返回head;
(3)个人觉得题目出的很不友好,当k=链表的长度时,返回的时原链表;当k大于链表的长度时,则不是。。。无法理解。因此我按照自己的思路,默认当k大于链表长度时,依然返回原链表。
(4)具体解决思路:首先引入一个头指针指向第一个结点,然后再引入两个指针first和second指针。first先于second向前移动k个结点,然后first和second同步向后移动,直到尾结点。
4. 代码实现
public class RotatedList61 {
public static void main(String[] args) {
ListNode head = new ListNode(1);
ListNode l2 = new ListNode(2);
ListNode l3 = new ListNode(3);
ListNode l4 = new ListNode(4);
ListNode l5 = new ListNode(5);
head.next = l2;
l2.next = l3;
l3.next = l4;
l4.next = l5;
l5.next = null;
ListNode l = rotateRight(head, 7);
do {
System.out.println(l.val);
l = l.next;
} while (l != null);
}
public static ListNode rotateRight(ListNode head, int k) {
if (head == null || head.next == null) return head;
ListNode headPoint = new ListNode(0);
ListNode first = headPoint;
ListNode second = headPoint;
headPoint.next = head;
// first指针先移动k个结点
while (k > 0) {
first = first.next;
if (first.next == null) return head;
k--;
}
// first、second同步向后移动
while (first.next != null) {
first = first.next;
second = second.next;
}
first.next = headPoint.next;
headPoint.next = second.next;
second.next = null;
return headPoint.next;
}
}
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
LeetCode: 61. Rotate List(Medium)的更多相关文章
- LeetCode: 60. Permutation Sequence(Medium)
1. 原题链接 https://leetcode.com/problems/permutation-sequence/description/ 2. 题目要求 给出整数 n和 k ,k代表从1到n的整 ...
- LeetCode:11. ContainerWithWater(Medium)
原题链接:https://leetcode.com/problems/container-with-most-water/description/ 题目要求:给定n个非负整数a1,a2,...,an ...
- LeetCode 48. Rotate Image(旋转图像)
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- 【leetcode】Rotate List(middle)
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
- 【leetcode】Rotate Image(middle)
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- LeetCode: 62. Unique Paths(Medium)
1. 原题链接 https://leetcode.com/problems/unique-paths/description/ 2. 题目要求 给定一个m*n的棋盘,从左上角的格子开始移动,每次只能向 ...
- LeetCode: 56. Merge Intervals(Medium)
1. 原题链接 https://leetcode.com/problems/merge-intervals/description/ 2. 题目要求 给定一个Interval对象集合,然后对重叠的区域 ...
- LeetCode: 55. Jump Game(Medium)
1. 原题链接 https://leetcode.com/problems/jump-game/description/ 2. 题目要求 给定一个整型数组,数组中没有负数.从第一个元素开始,每个元素的 ...
- LeetCode: 54. Spiral Matrix(Medium)
1. 原题链接 https://leetcode.com/problems/spiral-matrix/description/ 2. 题目要求 给定一个二维整型数组,返回其螺旋顺序列表,例如: 最后 ...
随机推荐
- React Native for Android on Windows 配置开发安装总结
配置开发安装总结(由于当前react-native更新较快,目前是针对2015年11月底时的reacti-native android for windows版本,有些内容可能过时) 官方的安装指导在 ...
- js实现抛物线运动 兼容IE低版本(转)
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...
- BIND简易教程(3):DNSSec配置
目录:BIND简易教程(1):安装及基本配置BIND简易教程(2):BIND视图配置BIND简易教程(3):DNSSec配置 (本篇) DNSSec,有个半英半中的名字叫DNS安全扩展.说的好听一点, ...
- non-fragile:oc2.0特性
Runtime Versions and Platforms There are different versions of the Objective-C runtime on different ...
- Kali Linux重设root密码
许久不用的Kali,某天打开竟忘了密码! 网上的方法颇为简单,遂准备亲自试一下. #光标移动到第二行的“恢复模式”,按E进入[编辑模式] ...
- 页面间传递前端请求参数和获取参数:Model model,HttpServletRequest request, ModelMap map参数使用与区别
Model model, HttpServletRequest request, ModelMap map声明变量 一.下面的方法是需要将请求发过来的数据(或者说参数)传递到重定向的页面/转发的页面的 ...
- sql获取时间、年龄
mysql数据库获取年龄:TIMESTAMPDIFF(YEAR, [出生日期字段], CURDATE()) select * from (select name 姓名,TIMESTAMPDIFF( ...
- C# abstract,virtual 修饰符
abstract(抽象):该abstract修饰符指示要修改的东西有缺失或不完整的实现.abstract修饰符可以与类,方法,属性,索引器和事件一起使用.abstract在类声明中使用修饰符来指示类仅 ...
- mysql事务隔离
一.事务的特性 原子性.一致性.隔离性.持久性 二.事务的隔离级别 1.未提交读 (Read Uncommitpeatableted) 臭名昭著的脏读 ,事务A读到事务B未提交的数据 2.提交读RC( ...
- oracle在线迁移同步数据,数据库报错
报需要升级的错误,具体处理步骤如下: 一.错误信息 SQL> alter database open ;alter database open resetlogs*ERROR at line 1 ...