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

有很多细节需要注意,比如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. BZOJ1857:[SCOI2010]传送带——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=1857 Description 在一个2维平面上有两条传送带,每一条传送带可以看成是一条线段.两条传送 ...

  2. BZOJ1022 [SHOI2008]小约翰的游戏John 【博弈论】

    1022: [SHOI2008]小约翰的游戏John Time Limit: 1 Sec  Memory Limit: 162 MB Submit: 3014  Solved: 1914 [Submi ...

  3. HDOJ.2111 Saving HDU (贪心)

    Saving HDU 点我挑战题目 题意分析 给出来背包容量v和物品数量n,接下来n行分别给出每个商品单位体积的价值和物品总共的体积(注意是单位体积,不是每个物品).求出最多能装多少价值的物品. 典型 ...

  4. lighttpd - 配置

    Lighttpd core 配置 connection.kbytes-per-second     限制每一个链接的速度etag.use-inode                   Etag使用i ...

  5. bzoj1057: [ZJOI2007]棋盘制作(悬线法)

    题目要求纵横坐标和奇偶性不同的点取值不同,于是我们把纵横坐标和奇偶性为1的点和0的点分别取反,就变成经典的最大全1子矩阵问题了,用悬线法解决. #include<iostream> #in ...

  6. [CQOI2017]小Q的表格——反演好题

    zhoutb2333的题解 难得一见的新颖反演题. 一眼看可能不是反演题. 修改影响别的,很恶心. 所以考虑化简f的联系式,发现和gcd有关 于是考虑用gcd来表示所有的gcd(a,b)=g的所有f( ...

  7. [技巧篇]16.MyEclipse2014安装SVN插件,在线安装

    这两天想做点东西,但是现在流行的是git,但是军哥的项目是托管到阿里的svn代码当中,所以没有办法,还需要弄SVN,这里不将什么安装SVN等东西 我要求的就是快速入门而已,仅此而已,我安装成功,过程很 ...

  8. JavaMail实现邮件的发送

    1,拷贝mail.jar 和activation.jar到项目中 2,开启邮箱的 POP3/SMTP服务,以QQ邮箱为例 进去QQ邮箱-->设置-->账号-->进行设置如下图 注意: ...

  9. 51Nod 1007 正整数分组 | DP (01背包)

    Input示例 5 1 2 3 4 5 Output示例 1 分析:2组的差最小,那么每一组都要接近sum/2,这样就转化成了普通的0 - 1背包了 #include <bits/stdc++. ...

  10. [Luogu 3966] TJOI 2013 单词

    经典ACAM. 注意单词之间添加字符,以及对重复单词的处理. #include <cstdio> #include <cstring> #include <queue&g ...