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. 题目要求 给定一个二维整型数组,返回其螺旋顺序列表,例如: 最后 ...
随机推荐
- IDEA 搭建Java WEB 开发环境
本文是一篇讲解如何在 目前比较流行的IntellJ IDEA 下搭建JavaWEB的说明文档, 如有写的不详细的地方,希望各位留下自己宝贵的意义. Tips : 遇到的问题 , 请耐心看完文章,在文章 ...
- IBM websphere MQ远程队列的简单配置
原理: 1.远程队列分发送方和接收方 2.接收方配置: 接收方配置要先拿到对方的发送通道配置,接收方的队列名称必须和远程发送方的队列名称一致,告诉远程发送方,你的地址,队列管理器名称等信息,在通道中建 ...
- PHP语言开发微信公众平台(订阅号)之curl命令(补充)
在之前的一篇随笔中,博主在调用curl命令上传文件时会经常出现上传方法过时的情况.如下图所示: 所以,我们只需要把上传方法换成创建CURLFile 类即可.如下所示 $ch = curl_init() ...
- redis 哈希数据类型简单操作(实现购物车案例)
这里不累赘如何安装redis和php redis扩展,主要熟悉调用redis哈希数据类型 简单方法操作如下 1:hSet 2:hGet 4:hDel 5:hGetAll 4:hExists 5:hI ...
- 使用 PHP Curl 做数据中转
流程 收集头部信息 收集请求数据 转换头部信息为 CURL 头部请求格式 使用 Curl 进行转发 收集 HTTP 头信息 function getAllHeaders() { $headers = ...
- .net 基础(一)
方法 只需要考虑2个 东西 1. 方法的参数 2.方法的返回值 当参数的个数不确定的时候,可以采用可变参数params. params 数组的 个数,不确定.当传入的 参数为空的时候,可变参数的数组 ...
- 关于JavaScript中省略元素对数组长度的影响
在学习<JavaScript权威指南>第六版的第7.1节中通过数组直接量创建数组时,我们可以不给数组的某个元素赋值,它就会使undefined.虽然是undefined,但我们调用数组对象 ...
- git 分支管理方案
现有一般的公司项目均使用git(大多数是gitLab)管理. 开发组 我们的项目都要建立在 开发组的名下 (git.xxcompany.com/xxgroup),除需要公司内部开源的项目,都必须设置为 ...
- 工具 | Axure基础操作 No.2
不废话了,直接如之前一样上操作图才是正道. 1.设置文本类型为密码或者文件类型 可以在属性中也选择最大长度制定长度. 如果设置类型为文件,在浏览器中就会自动变成选择本地文件的按钮. 2.文本框提示文字 ...
- Swift_继承
Swift_继承 点击查看源码 func testInheritance() { //基类 class Base { var count = 0.0 var description: String { ...