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. No module named _sqlite3

    [root@lgj01 opsadmin]# python manage.py startapp accountTraceback (most recent call last):  File &qu ...

  2. hdu-3524 Perfect Squares---打表+找规律+循环节

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3524 题目大意: 求i^2 mod 2^n有多少可能 解题思路: 先打表,求出n较小的时候的数据 n ...

  3. Python取出SQL表单中的字段名

    def ReturnInfo(self, avalue, akey): cursor = connection.cursor() Sql = "select * from %s where ...

  4. Avito Cool Challenge 2018 C. Colorful Bricks 【排列组合】

    传送门:http://codeforces.com/contest/1081/problem/C C. Colorful Bricks time limit per test 2 seconds me ...

  5. 14、SpringBoot-CRUD错误处理机制(1)

    一.springboot默认的处理机制 1.浏览器返回一个错误的页面 默认处理错误:返回一个错误的页面: 包括错误类型.时间......   2.其他客户端访问 默认响应一个json数据 原理: 错误 ...

  6. JDBC(6)事务处理&批量处理

    事务处理就是当执行多个SQL指令,因某个指令有误,则取消执行所有的命令 它的作用是保证各项的完整性和一致性 JDBC的数据操作时 commit():提交事务 rollback():回退事务 绝位于ja ...

  7. px,rem,em的区别

    PX特点 1. IE无法调整那些使用px作为单位的字体大小: 2. 国外的大部分网站能够调整的原因在于其使用了em或rem作为字体单位: 3. Firefox能够调整px和em,rem,但是96%以上 ...

  8. .NET 中 如果一个Task A正在await另一个Task B,那么Task A是什么状态

    新建一个.NET Core控制台程序,输入如下代码: using System; using System.Threading; using System.Threading.Tasks; class ...

  9. js的匿名函数与自定义函数

    //匿名方法,会执行,自己调用自己 (function () { console.log(window.innerHeight); })(); (function () { console.log(w ...

  10. 使用BSRR和BRR寄存器直接操作STM32的I/O端口

    STM32的每个GPIO端口都有两个特别的寄存器,GPIOx_BSRR和GPIOx_BRR寄存器,通过这两个寄存器可以直接对对应的GPIOx端口置'1'或置'0'. GPIOx_BSRR的高16位中每 ...