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)的更多相关文章

  1. LeetCode: 60. Permutation Sequence(Medium)

    1. 原题链接 https://leetcode.com/problems/permutation-sequence/description/ 2. 题目要求 给出整数 n和 k ,k代表从1到n的整 ...

  2. LeetCode:11. ContainerWithWater(Medium)

    原题链接:https://leetcode.com/problems/container-with-most-water/description/ 题目要求:给定n个非负整数a1,a2,...,an  ...

  3. LeetCode 48. Rotate Image(旋转图像)

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  4. 【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 ...

  5. 【leetcode】Rotate Image(middle)

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  6. LeetCode: 62. Unique Paths(Medium)

    1. 原题链接 https://leetcode.com/problems/unique-paths/description/ 2. 题目要求 给定一个m*n的棋盘,从左上角的格子开始移动,每次只能向 ...

  7. LeetCode: 56. Merge Intervals(Medium)

    1. 原题链接 https://leetcode.com/problems/merge-intervals/description/ 2. 题目要求 给定一个Interval对象集合,然后对重叠的区域 ...

  8. LeetCode: 55. Jump Game(Medium)

    1. 原题链接 https://leetcode.com/problems/jump-game/description/ 2. 题目要求 给定一个整型数组,数组中没有负数.从第一个元素开始,每个元素的 ...

  9. LeetCode: 54. Spiral Matrix(Medium)

    1. 原题链接 https://leetcode.com/problems/spiral-matrix/description/ 2. 题目要求 给定一个二维整型数组,返回其螺旋顺序列表,例如: 最后 ...

随机推荐

  1. postgresql+postgis+pgrouting实现最短路径查询(2)---openlayers+geoserver实现最短路径

    自己的最短路径实现基本上是按照参考博文的1.2和3进行的,实现的时候也是问题不断,只能是一个一个解决. 问题1:自己发布的geoserver服务无法和OSM底图叠加到一起. 解决:参考博文2提到发布服 ...

  2. HTML DOM 初学笔记

    JavaScript HTML DOM 当网页被加载时,浏览器会创建页面的文档对象模型(Document Object Model). HTML DOM 模型被构造为对象的树,如图: 通过可编程的对象 ...

  3. sysctl.conf学习和调优

    转载于简书:sysctl.conf学习和调优 ,如有版本问题,请联系我 前言 记得第一次接触/etc/security/limits.conf和/etc/sysctl.conf时是因为部署Oracle ...

  4. Yii: 扩展CGridView增加导出CSV功能

    Yii提供的CGridView组件没有内置数据导出功能,不过我们可以通过扩展该组件来添加该功能. 具体方法如下: 1.首先派生一个子类,添加一个action成员,在该视图的init函数中判断是浏览动作 ...

  5. 提交文件到ng-pages分支

    一.提交dist文件夹到ng-pages分支 git subtree push --prefix=dist origin gh-pages 二.提交所有文件到ng-pages分支 text git:( ...

  6. mysql学习之join用法

    转载  一张图看懂 SQL 的各种 join 用法 一.JOIN 使用介绍 下面例子使用的数据表如下: -- ---------------------------- -- Table structu ...

  7. 二. Python WebDriver环境搭建

    1. 安装Selenium 在命令行中输入: 显示安装成功: 2. 测试例子 打开百度页面并在输入框输入搜索内容(默认为firework) # 1. Selenium默认为Firefox.验证 fro ...

  8. Entity Framework中DbContext结合TransactionScope提交事务的正确方式

    问: I would like know what is the best possible way to implement transactions with DBContext. In part ...

  9. webpack报错Cannot read property 'presetToOptions' of undefined

    在学习react全家桶时,webpack首先报错,报错内容如下,最后我是因为没有全局安装webpack导致的报错,使用npm install webpack -g安装解决了这个问题.

  10. JS中的“==”与强制类型转换

    JavaScript中有“==”与“===”,那么他们有何区别呢? 对于基本数据类型, ===  (!==)只有当两个变量的类型和值都相等时,才返回true:而 == (!=)则会对变量进行强制类型转 ...