Given a list, rotate the list to the right by k places, where k is non-negative.

For example:
Given 1->2->3->4->5->NULL and k = 2,
return 4->5->1->2->3->NULL.

题意:

翻转链表。

思路:

首先需要读懂题意。题目的描述有问题,应该是将链表的最后k个元素移动到链表的头部。

这道题的本质就是寻找链表的倒数第k个元素。在该点将链表分成两个部分,然后调换顺序即可。

陷阱:

k的长度没有说明,可能k比链表的长度还要大。 设链表的长度为Len,那么移动的节点个数应该为 K%Len.

  

class Solution {
public:
int getListLength(ListNode *head){
int len=;
ListNode *tmp=head;
while(tmp){
len++;
tmp = tmp->next;
}
return len;
}
ListNode *rotateRight(ListNode *head, int x) {
if(head==NULL) return head; // empty list
int len = getListLength(head);
x = x%len; // how many nodes to rotate
if( len<= || x==) return head; // find xth node from tail of list
ListNode *tmp=head;
ListNode *lend=head;
ListNode *hstart=head;
while(tmp->next){
if(x==){
lend = lend->next;
}else{
--x;
}
tmp = tmp->next;
} hstart = lend->next;
lend->next=NULL;
tmp->next=head;
return hstart;
}
};

转载请注明出处: http://www.cnblogs.com/double-win/谢谢!

[LeetCode 题解]: Rotate List的更多相关文章

  1. 【LeetCode题解】二叉树的遍历

    我准备开始一个新系列[LeetCode题解],用来记录刷LeetCode题,顺便复习一下数据结构与算法. 1. 二叉树 二叉树(binary tree)是一种极为普遍的数据结构,树的每一个节点最多只有 ...

  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题解-122买卖股票的最佳时期

    题目 leetcode题解-122.买卖股票的最佳时机:https://www.yanbinghu.com/2019/03/14/30893.html 题目详情 给定一个数组,它的第 i 个元素是一支 ...

  4. 【LeetCode题解】3_无重复字符的最长子串(Longest-Substring-Without-Repeating-Characters)

    目录 描述 解法一:暴力枚举法(Time Limit Exceeded) 思路 Java 实现 Python 实现 复杂度分析 解法二:滑动窗口(双指针) 思路 Java 实现 Python 实现 复 ...

  5. 【LeetCode题解】225_用队列实现栈(Implement-Stack-using-Queues)

    目录 描述 解法一:双队列,入快出慢 思路 入栈(push) 出栈(pop) 查看栈顶元素(peek) 是否为空(empty) Java 实现 Python 实现 解法二:双队列,入慢出快 思路 入栈 ...

  6. 【LeetCode题解】232_用栈实现队列(Implement-Queue-using-Stacks)

    目录 描述 解法一:在一个栈中维持所有元素的出队顺序 思路 入队(push) 出队(pop) 查看队首(peek) 是否为空(empty) Java 实现 Python 实现 解法二:一个栈入,一个栈 ...

  7. 【LeetCode题解】844_比较含退格的字符串(Backspace-String-Compare)

    目录 描述 解法一:字符串比较 思路 Java 实现 Python 实现 复杂度分析 解法二:双指针(推荐) 思路 Java 实现 Python 实现 复杂度分析 更多 LeetCode 题解笔记可以 ...

  8. 【LeetCode题解】25_k个一组翻转链表(Reverse-Nodes-in-k-Group)

    目录 描述 解法一:迭代 思路 Java 实现 Python 实现 复杂度分析 解法二:递归(不满足空间复杂度) 思路 Java 实现 Python 实现 复杂度分析 更多 LeetCode 题解笔记 ...

  9. 【LeetCode题解】24_两两交换链表中的节点(Swap-Nodes-in-Pairs)

    目录 描述 解法一:迭代 思路 Java 实现 Python 实现 复杂度分析 解法二:递归(不满足空间复杂度要求) 思路 Java 实现 Python 实现 复杂度分析 更多 LeetCode 题解 ...

随机推荐

  1. Spring Mvc:用MultiPartFile上传单个文件,多个文件

    1.单个文件上传步骤: 添加Apache文件上传jar包 首先需要下载两个apache上传文件的jar包,commons-fileupload-1.3.1jar,commons-io-2.4.jar ...

  2. [html]window.open 使用示例

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

  3. [置顶] caffe+CPU︱虚拟机+Ubuntu16.04+CPU+caffe安装笔记

    由于虚拟机下的Ubuntu系统一般不包含GPU,故这次安装时为了在无GUP环境下运行caffe.所以只需安装CPU版本的caffe 由于本机是window10系统,所以想尝试caffe就在自己电脑上整 ...

  4. javascript中不存在块级作用域,所以要小心使用在块级作用域中的函数声明所带来的作用域混乱.

    在javascript中函数的作用域是一个非常重要的概念. javascript中是没有块级作用域,但是有函数作用域的概念. 我们在开发的过程中,经常会遇到这样的问题, 某个函数我暂时不需要,不想声明 ...

  5. nginx.conf_2017-11-24

    user webroot; worker_processes 4; worker_cpu_affinity 1000 0100 0010 0001; worker_rlimit_nofile 6550 ...

  6. 记录在Python2.7 x64 bit 下 PyQt5.8的编译过程

    由于工作需要使用python下面的Qt库.PyQt现在只提供针对Python3.X系列的PyQt,所有需要自己手动编译.防止忘记,特意写下随笔记录备忘. 工 作  环境:Python版本:Python ...

  7. delphi加密算法

    function EncryptKey(sUser,sPasswd:string):string;stdcall; var __i,__k,__a : Integer; __s : string; b ...

  8. VB指针操作和消息钩子

    二.VB怎么用指针       要想弄明白VB怎么使用指针,就必须要弄明白两件事,第一,如何取得数组的指针,第二,如何将指针所指向的数组取出来.       A.在讲解这两个问题之前,我们需要了解几个 ...

  9. Changing the load order/delay the start of the Server service

    THE INFORMATION IN THIS ARTICLE APPLIES TO: Secure FTP Server (All Versions) EFT Server (All Version ...

  10. javax.persistence.RollbackException: Error while committing the transaction

    the valid jpa update entity code gives the exception below in the case of  wrong dependency( org.hib ...