题目:给定链表,和一个k,把链表的后k个旋转到前头,例如链表为: 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL.

一开始我想,跟将后面第k个元素删除一样,可以遍历一次就可以解决问题。但是在测评的时候发现k有大于链表长度的时候,我以为如果大于长度了就是不用翻转了,原来不是这样。如果k大于链表的长度了,那么就再从尾部开始往后一直到k为止,也就是我们可以看做在将k对链表长度取模之后的k才是一个链表从后往前的第k个。这样的话我们可以如下步骤做:

1.计算链表长度len

2.将k%=len

3.从后往前是第k个,那从前往后就是第len-k个,找到这个元素的前一个,然后再找到最后一个元素,将最后一个元素的next指向原来的head,然后将第k个元素设置为head,然后再讲第k-1个的next设置为NULL,大功告成。

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *rotateRight(ListNode *head, int k) {
if(head == NULL || k==) return head;
ListNode *right = head, *left = head;
int len = ;
while(right =right ->next) len++;
k %= len;
k = len - k;
right = head;
while(--k > ) right = right->next;
while(--len > ) left = left->next;
left->next = head;
head = right->next;
right->next = NULL;
return head;
}
};

leetcode[60] Rotate List的更多相关文章

  1. LeetCode:60. Permutation Sequence,n全排列的第k个子列

    LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...

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

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

  3. [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 ...

  4. [Leetcode][048] Rotate Image 略详细 (Java)

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

  5. 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  ...

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

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

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

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

  8. 【LeetCode】 Rotate List 循环链表

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

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

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

随机推荐

  1. hdu4570Multi-bit Trie (间隙DP)

    Problem Description IP lookup is one of the key functions of routers for packets forwarding and clas ...

  2. windows 10 install oracle 12c error:[ INS-30131 ]

    "[ INS-30131 ] the Initial Setup That Is Required to Run the Installation Program Validation Wa ...

  3. mount命令使用具体解释(Linux)

    linux是一个优秀的开放源代码的操作系统,能够执行在大到巨型小到掌上型各类计算机系统上,随着 linux系统的日渐成熟和稳定以及它开放源代码特有的优越性,linux在全世界得到了越来越广泛的应用. ...

  4. 024找到二维阵列(keep it up)

    剑指offer在标题中:http://ac.jobdu.com/problem.php? pid=1384 题目描写叙述: 在一个二维数组中,每一行都依照从左到右递增的顺序排序.每一列都依照从上到下递 ...

  5. 于PsIsSystemThread无论是在线程系统线程标识获得

    我一直好奇一个进程的所有线程改变线程标志Terminated mov edi, edi ; IoIsSystemThread push ebp mov ebp, esp mov eax, [ebp+a ...

  6. CentOS7 安装spark集群

    Spark版本 1.6.0 Scala版本 2.11.7 Zookeeper版本 3.4.7 配置虚拟机 3台虚拟机,sm,sd1,sd2 1. 关闭防火墙 systemctl stop firewa ...

  7. NSIS:制作软件升级安装包

    原文 NSIS:制作软件升级安装包 相信不是每个人写的软件都只发布一次就可以了,肯定要有修改和维护的情况发生.在这种情况下,您可能就需要一个软件的升级安装包了.   现在,我们就来一步步把这个安装包做 ...

  8. Android分享到微信等社交平台教程

    在Android手机app上增加分享到微信等平台的功能,使用的是第三方的开源组件,友推SDK. 集成分为下面几个步骤: 1. 在友推官网注冊,加入要集成友推sdk的 app信息,并获取appkey. ...

  9. hibernate学习笔记(1)hibernate基本步骤

    hibernate基本步骤 1.创hibernate置对象 Configuration config = newConfiguration(); config.configure("hibe ...

  10. 开源TinyXML 最简单的新手教程

    TinyXML它是基于一个非常受欢迎的现在DOM型号XML解析器,简单易用且小巧玲珑,很适合存储简单数据.配置文件. 该项目属于开源项目,在sourceforge上边的链接是:http://sourc ...