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. 题目要求 给定一个二维整型数组,返回其螺旋顺序列表,例如: 最后 ...
 
随机推荐
- Yii  日志组件
			
详细的介绍查看官网的document:http://www.yiiframework.com/doc/guide/1.1/en/topics.logging 也可以看 Yii 1.1 Applicat ...
 - 第八章.Spring MVC
			
基于MyEclipse开发 工程结构: 所使用到的jar: 代码: FruitControllerTest.java public class FruitControllerTest implemen ...
 - $.ajax方法success方法窗口弹不出
 - 基于VC++的WinCE网口通信
			
基于VC++的WinCE网口通信 WinCE下的网络编程与Windows下的非常类似,只是个别API函数有所不同.同样分为UDP和TCP两种,UDP就是无连接的通信,通过“用户数据报协议”(UDP)来 ...
 - Unity透明视频播放 所需的Shader脚本
			
Shader "Custom/ShaderMovie" { Properties { _MainTex("Color (RGB)", 2D) = "b ...
 - Office365学习笔记—Xslt自定义列表视图
			
1,在Office365中需要添加自定义的视图!用Spd添加视图,这儿我添加一个testView! (1)打开testView.aspx将</ZoneTemplate>节点中的内容全部删除 ...
 - Python 学习笔记(十)Python集合(二)
			
集合常用的方法 add() 向集合中增加一个元素,如果集合中已经有了这个元素,那个这个方法就会失效 >>> help(set.add) Help on method_de ...
 - python基础  列表和字符串
			
1.列表和字符串最大区别就是 列表可以改变,字符串不可以改变 列表: welldone = [ "hutu","python","java" ...
 - oracle查询相关注意点
			
单表查询: .or 和 and 混合使用 需求:查询业主名称包含'刘'或门牌号包含'5'的,并且地址编号为3的记录 and 的权限优先于 or 所以需要在or的两边添加() 2. 范围查询 除了传统的 ...
 - iOS | 解决中文乱码
			
在iOS开发中,多多少少的朋友在开发的过程中,测试数据的时候可能会碰到后台打印的时候不能正确的打印出正常的汉字,打印出一些影响判断的字符,经常需要查看数组中得元素是否是自己想要的,但是苹果并没有对直接 ...