[抄题]:

给定一个链表,旋转链表,使得每个节点向右移动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. 1114 Family Property (25 分)

    1114 Family Property (25 分) This time, you are supposed to help us collect the data for family-owned ...

  2. python 网页抓取并保存图片

    #-*-coding:utf-8-*- import os import uuid import urllib2 import cookielib '''获取文件后缀名''' def get_file ...

  3. [Java基础] Java float保留两位小数或多位小数

    方法1:用Math.round计算,这里返回的数字格式的. float price=89.89; int itemNum=3; float totalPrice=price*itemNum; floa ...

  4. C# 控制台程序(Console Application )启动后隐藏

    背景 前段时间给项目编写了数据适配器,读取其他系统的数据后推送到MQ上,我们的系统通过订阅MQ的方式来获取.由于其他系统支持C#编程,且为了一时方便,选择了C#的控制台程序. 最近用户在使用中,总是不 ...

  5. javascript节点操作appendChild()

    cloneNode(a)方法接受一个布尔值参数,表示是否深拷贝 true:表示执行深拷贝,复制本节点以及整个子节点树. false:浅拷贝.只复制节点本身. 复制后返回的节点副本属于文档所有,但是并没 ...

  6. 第7章 进程关系(5)_贯穿案例2:mini shell(2)

    5. 贯穿案例2:mini shell(2) (1)己经完成的功能:pwd.cd.exit命令 (2)阶段性目标: ①env.export.echo及其他命令 ②标准输入.输出重定向"> ...

  7. final修饰的类有什么特点

    变量定义为final,一旦被初始化便不可改变,这里不可改变的意思对基本类型来说是其值不可变,而对于对象变量来说其引用不可再变. 方法定义为final,是为了防止任何继承类改变它. 类定义为final, ...

  8. HTML5 Canvas ( 圆和切点曲线的绘制 ) arc, arcTo

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. playbook相关

    ansible-playbook site.yml  -f 10 ansible-playbook常用参数说明: -f  10          启用10个并发进程数执行playbook -u  RM ...

  10. MySQL的事务处理及隔离级别

      事务是DBMS得执行单位.它由有限得数据库操作序列组成得.但不是任意得数据库操作序列都能成为事务.一般来说,事务是必须满足4个条件(ACID)       原子性(Autmic):事务在执行性,要 ...