[抄题]:

给定一个链表,旋转链表,使得每个节点向右移动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. sklearn的GridSearchCV例子

    class sklearn.model_selection.GridSearchCV(estimator, param_grid, scoring=None, fit_params=None, n_j ...

  2. Parallel I/O and Columnar Storage

    Parallel I/O and Columnar Storage We begin with a high level overview of the system while follow up ...

  3. C#中char空值的几种表示方式

    C#中char空值的几种表示方式 在C#中char类型的表示方式通常是用单引号作为分隔符,而字符串是用双引号作为分隔符. 例如: 程序代码 程序代码 char a = 'a'; char b = 'b ...

  4. java的多态性(二)

    2013-10-16 19:44 9364人阅读 评论(25) 收藏 举报  分类: [JAVA开发]-----Java提高篇(36)  版权声明:本文为博主原创文章,未经博主允许不得转载.   目录 ...

  5. django的用户认证组件

    DataSource:https://www.cnblogs.com/yuanchenqi/articles/9064397.html 代码总结: 用户认证组件: 功能:用session记录登录验证状 ...

  6. Spark运行模式概述

    Spark编程模型的回顾 spark编程模型几大要素 RDD的五大特征 Application program的组成 运行流程概述 具体流程(以standalone模式为例) 任务调度 DAGSche ...

  7. 外观设计模式 (Facade)

    目的:为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使 外观设计模式使用场合: 1. 在设计初期阶段,应该有意识的将不同的两个分层.层与层之间建立外观 ...

  8. HAproxy使用

    参考官网 安装HAproxy/ pull 官方镜像 本地安装:本地安装路径:/usr/local/haproxy/配置: 添加:/usr/local/haproxy/conf/haproxy.cfg添 ...

  9. nginx支持pathinfo的方法,亲测有效的

    修改配置文件,修改特点域名的配置文件 location ~ \.php { #去掉$ root H:/PHPServer/WWW; fastcgi_pass ; fastcgi_index index ...

  10. c# 导入导出excel表格式

    c#使用代码导入excel时,当遇到纯数字且大于15位时会出现编码混乱(表现为科学计数法),要想呈现与excel表中纯数字格式和在数据库中呈现纯数字,操作如下: 完成即可. 导出取决于导入的内容排版.