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. Android(java)学习笔记207:Android下的属性动画(Property Animation)

    1. 属性动画(Property Animation)引入: 在手机上去实现一些动画效果算是件比较炫酷的事情,因此Android系统在一开始的时候就给我们提供了两种实现动画效果的方式,逐帧动画(fra ...

  2. ADF中VO的删除操作初探

    在ADF的VO中,真实提交更改是在commit 方法执行之后,如以下增加操作 EntityDefImpl departmentEODef = DepartmentEOImpl. getDefiniti ...

  3. 关于<meta>的各种用处以及移动端的常见问题

    1.优先使用最新版本的IE和Chrome <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1& ...

  4. 如何查看Windows下端口占用情况

    开始---->运行---->cmd,或者是window+R组合键,调出命令窗口  输入命令:netstat -ano,列出所有端口的情况.在列表中我们观察被占用的端口,比如是49157,首 ...

  5. IP Addressing

    IP Addressing(处理) Each host on Internet has unique 32 bit IP address Each address has two parts: net ...

  6. 【Nginx】使用Nginx作为Http代理的配置文件

    请看配置文件中的注释~ #user nobody; worker_processes 1; #pid logs/nginx.pid; events { worker_connections 1024; ...

  7. GIT 报错:Result too large 解决办法

    在使用bower install命令下载前端依赖的js插件时,git出错了,报错信息如下: bower ECMDERR Failed to execute "git ls-remote -- ...

  8. CF考古活动

    Codeforces Beta Round #1 http://codeforces.com/contest/1 A.测试用水题,呵呵.给三个数nma,求ceil(n/a)*ceil(m/a). 长整 ...

  9. MySQL---事务、函数

    事务 事务用于将某些操作的多个SQL作为原子性操作,一旦有某一个出现错误,即可回滚到原来的状态,从而保证数据库数据完整性. delimiter \\ create PROCEDURE p1( OUT ...

  10. TinyMCE:下载、安装、配置

    第一步:下载 官网下载:https://www.tiny.cloud/download/ TinyMCE从4.0开始,不再支持直接下载,而是直接使用提供免费的CDN,让用户免除安装过程,可以在网站中使 ...