这题我的第一想法是用头插法,但实际上并不好做,因为每次都需要遍历最后一个。更简单的做法是将其连成环,找到相应的位置重新设头结点和尾结点。这过

有很多细节需要注意,比如K有可能是大于链表长度的,如何重新设置K等都要注意。

 ListNode *rotateList(ListNode *head, int k)
{
if (head == nullptr || k == )return head; ListNode *p = head;
int n = ;
while (p->next)
{
n++;
p = p->next;
} k =n - k%n;//k有可能大于n
p->next = head;
for (int i = ; i < k; i++)
p = p->next; head = p->next;
p->next = nullptr; return head;
}

leetcode 之Rotate List(18)的更多相关文章

  1. [array] leetcode - 48. Rotate Image - Medium

    leetcode - 48. Rotate Image - Medium descrition You are given an n x n 2D matrix representing an ima ...

  2. [LeetCode] 61. 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][048] Rotate Image 略详细 (Java)

    题目在这里 https://leetcode.com/problems/rotate-image/ [个人分析] 这个题目,我觉得就是考察基本功.考察细心的,算法方面没有太多东西,但是对于坐标的使用有 ...

  4. LeetCode 189. Rotate Array (旋转数组)

    Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array  ...

  5. LeetCode 48. Rotate Image(旋转图像)

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  6. LeetCode 48 Rotate Image(2D图像旋转问题)

    题目链接: https://leetcode.com/problems/rotate-image/?tab=Description   Problem:给定一个n*n的二维图片,将这个二维图片按照顺时 ...

  7. 【LeetCode】 Rotate List 循环链表

    题目:rotate list 解法1: <span style="font-size:18px;">/**LeetCode Rotate List:Given a li ...

  8. [LeetCode] 189. Rotate Array 旋转数组

    Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: ...

  9. [LeetCode] 48. Rotate Image 旋转图像

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

随机推荐

  1. UVA.548 Tree(二叉树 DFS)

    UVA.548 Tree(二叉树 DFS) 题意分析 给出一棵树的中序遍历和后序遍历,从所有叶子节点中找到一个使得其到根节点的权值最小.若有多个,输出叶子节点本身权值小的那个节点. 先递归建树,然后D ...

  2. 【状压DP】【P3959】【NOIP2017D2T2】宝藏

    Description 参与考古挖掘的小明得到了一份藏宝图,藏宝图上标出了 $n$ 个深埋在地下的宝藏屋, 也给出了这 $n$ 个宝藏屋之间可供开发的$ m$ 条道路和它们的长度. 小明决心亲自前往挖 ...

  3. 第三方库安装——lxml

    环境 操作系统:CentOS 6.7 32-bit Python:2.6.6 安装 安装依赖软件 yum -y install gcc make python-devel libxml2-devel ...

  4. HDU 1535 SPFA 前向星存图优化

    Invitation Cards Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  5. 网络编程:I/O模型

    I/O模型 Unix下可用的5种I/O模型有: 阻塞式I/O 非阻塞式I/O I/O复用(select和poll,epoll) 信号驱动式I/O 异步I/O(POSIX的aio_系列函数) 一个输入操 ...

  6. CharSequence 去除两端空格

    CharSequence就是字符序列,String, StringBuilder和StringBuffer都是其实现类. 模仿String.trim() 实现了一个CharSequence通用的去除两 ...

  7. Struts2.X深入浅出 学习笔记

    第一节.MVC以及Struts2简介 第二节.Action生命周期以及接收表单数据 第三节.Struts2配置以及Struts.xml详解 Struts2 主要配置文件 Web.xml 设置过滤器以及 ...

  8. 记一次rsync日志报错directory has vanished

    中午两点的时候邮件告知rsync同部svn源库失败,看rsync日志报错显示如上,当时还在上课,没在公司,怀疑是不是有人动了svn的版本库,后来询问同事并通过vpn登录服务器上查看版本库是正常的,也没 ...

  9. Item 2---遇到构造器具有多个参数时,要考虑用构建器;Builder模式

    问题,面对这种一个构造器具备多个参数的问题,现有的做法是使用重叠构造器的方式,该方式存在的问题: public class NutritionFacts { private final int ser ...

  10. MongoDB安装配置及使用

    1.安装配置:https://www.cnblogs.com/ymwangel/p/5859453.html 2.使用 from pymongo import MongoClient #连接 conn ...