[抄题]:

给定一个链表,旋转链表,使得每个节点向右移动k个位置,其中k是一个非负数

样例

给出链表1->2->3->4->5->null和k=2

返回4->5->1->2->3->null

[思维问题]:

就是两条线段的变种,想不到联系

[一句话思路]:

用线段找到位置n,然后连一下

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

连接的时候:

head.next = dummy.next;
dummy.next = tail;(错了)

1->2->3->2->1->null
1

wrong:
2->null
right:
1->1->2->3->2->null
[二刷]:
  1. 因为 head = dummy;往前移动了一位,快指针退出的条件是后一位非空while(head.next != null)
  2. tail = dummy;慢指针一定要初始化为dummy

  3. [总结]:

    if (head == null) {
    return null;
    }

头节点判断空没写就全错了,自己掂量着办吧

[复杂度]:Time complexity: O(n) Space complexity: O(1)

[英文数据结构,为什么不用别的数据结构]:

[其他解法]:

[Follow Up]:

[题目变变变]:

Search in Rotated Sorted Array,   在重复或者不重复的旋转数组中找数,或者求恢复。本题只讨论旋转数组是怎么制造出来的。

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/ public class Solution {
/*
* @param head: the List
* @param k: rotate to the right k places
* @return: the list after rotation
*/
//calculate
private int getLength(ListNode head) {
if (head == null) {
return 0;
}
int index = 0;
while(head != null) {
index++;
head = head.next;
}
return index;
} public ListNode rotateRight(ListNode head, int k) {
if (head == null) {
return null;
}// //find n
int length = getLength(head);
k = k % length;
ListNode dummy = new ListNode(0);
dummy.next = head;
head = dummy;
ListNode tail = dummy;
for(int i = 0; i < k; i++){
head = head.next;
}
//ListNode tail = dummy;
while(head.next != null) {
head = head.next;
tail = tail.next;
}
//join up
head.next = dummy.next;
dummy.next = tail.next;
tail.next = null; return dummy.next;
}
}

旋转链表(所有元素往右移) rotate list的更多相关文章

  1. [LeetCode] Rotate List 旋转链表

    Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...

  2. [Swift]LeetCode61. 旋转链表 | Rotate List

    Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: I ...

  3. LeetCode 61:旋转链表 Rotate List

    ​给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. Given a linked list, rotate the list to the right by k pla ...

  4. LeetCode 61. 旋转链表(Rotate List)

    题目描述 给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: 1->2->3->4->5->NULL, k = 2 输出 ...

  5. Leetcode61. Rotate List旋转链表

    给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: 1->2->3->4->5->NULL, k = 2 输出: 4-& ...

  6. LeetCode(61):旋转链表

    Medium! 题目描述: 给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: 1->2->3->4->5->NULL, ...

  7. PAT 乙级 1008 数组元素循环右移问题 (20) C++版

    1008. 数组元素循环右移问题 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 一个数组A中存有N(N>0)个整数,在不允 ...

  8. 【LeetCode题解】61_旋转链表(Rotate-List)

    目录 描述 解法:双指针 思路 Java 实现 Python 实现 复杂度分析 描述 给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: 1-> ...

  9. 【PAT】1008. 数组元素循环右移问题 (20)

    1008. 数组元素循环右移问题 (20) 一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M>=0)个位置,即将A中的数据由(A0A1……AN- ...

随机推荐

  1. [UE4]添加蒙太奇动画

    选择蒙太奇所使用的骨骼

  2. [UE4]C++创建对象的三种方式

    #include <iostream> using namespace std; class A { private: int n; public: A(int m):n(m) { } ~ ...

  3. Linux下安装Nginx依赖包和Nginx的命令

    1.安装依赖包pcrecd /usr/local/srcwget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.39.tar ...

  4. [UE4]Visual Studio的相关插件安装:UE4.natvis和UnrealVS Extension

    转自:http://aigo.iteye.com/blog/2281182 UE4.natvis 官方文档: https://docs.unrealengine.com/latest/INT/Prog ...

  5. golang web框架 beego 学习 (四) 连接mysql

    1 DB参数配置在app.conf appname = gowebProject httpport = runmode = dev [db] host= localhost port= databas ...

  6. win10安装.net framework3.5

    win10默认没有安装.net framework3.5,一般方法需提取Windows安装镜像,麻烦. 离线安装方法如下: cab格式.NET Framework 3.5离线安装包下载地址:百度网盘 ...

  7. ajaxGet 获取封装

    callback 表示下一个功能(回调函数) function ajaxGet(url,callback,data){           如果路径上有参数  就在url后面拼接参数 否则只请求url ...

  8. uva-10152-乌龟排序

    求从待排序的到期望的顺序的最小操作顺序,只能进行一个操作,将当前的乌龟拿出来,上面的下移,拿出来的放到最上面 发现voj没有PE, 解题方法,把俩个串反过来使用,从期望的顺序到待排序的顺序. AC:1 ...

  9. UVA375

    题意: 已知等腰三角形的高H,底边长B,这时有一个内切圆C, 以内切圆C和长度为B对应的角的角平分线的交点做切线. 切线与角平分线相交,此时切线,和俩边又会出现一个小的等腰三角形,也有一个小的内切圆C ...

  10. maven 在pom.xml 中指定仓库位置

    ...... 在pom.xml 中添加 仓库位置(这样遇到私服没有的依赖,就会去这下载) </properties> <repositories><!-- 代码库 --& ...