题目:rotate list

解法1:

<span style="font-size:18px;">/**LeetCode Rotate List:Given a list, rotate the list to the right by k places, where k is non-negative.
* 题目:循环移动链表,等价于将链表从右边数的k个节点移动到表的前方
* 思路:移动倒是简单。重点是要找到链表倒数的k个数,就等价于找到倒数第K+1个数,设置两个指针,先后遍历链表。中间相隔k个数
* 当前面的指针走到最后的一个节点,此时后面的指针指向的就是倒数第k+1个数
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
package javaTrain; public class Train14 {
public ListNode rotateRight(ListNode head, int k) {
ListNode pFast,pSlow,pKnode;
int n = 0; if(head == null || k < 1 ) return head; //注意特殊情况
pFast = head;
pSlow = head;
while(pFast != null){
pFast = pFast.next;
n++;
}
k = k%n; //循环移动,能够转变为求模
if(k == 0) return head; //移动的次数等于自己的长度。等价于本身
pFast = head;
while(k>0 && pFast != null){
pFast = pFast.next;
k--;
}
while(pFast.next != null){
pFast = pFast.next;
pSlow = pSlow.next;
}
pKnode = pSlow.next; //第k+1个节点,次后就是要移到前面的节点了,
pSlow.next = null;
pFast.next = head; //原本最后的节点此时排在头结点之前 return pKnode;
}
}
</span>

解法2:

<span style="font-size:18px;">//法2:将链表连城环,而后从新寻找新的头结点和尾节点,即在len-k处
package javaTrain; public class Train14_1 {
public ListNode rotateRight(ListNode head, int k) {
if(head == null || k == 0) return head; //特殊情况
ListNode pNode = head;
int len = 1;
while(pNode.next != null){
pNode = pNode.next;
len++;
}
k = len-k%len;
pNode.next = head; //注意此时pNode是原来的尾节点
for(int i = 0;i < k;i++){
pNode = pNode.next;
}
head = pNode.next;
pNode.next = null;
return head;
}
}
</span>

【LeetCode】 Rotate List 循环链表的更多相关文章

  1. C++ STL@ list 应用 (leetcode: Rotate Array)

    STL中的list就是一双向链表,可高效地进行插入删除元素. List 是 C++标准程式库 中的一个 类 ,可以简单视之为双向 连结串行 ,以线性列的方式管理物件集合.list 的特色是在集合的任何 ...

  2. [LeetCode] Rotate Array 旋转数组

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

  3. [LeetCode] Rotate List 旋转链表

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

  4. [LeetCode] Rotate Image 旋转图像

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

  5. LeetCode——Rotate List

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

  6. [LeetCode]Rotate Image(矩阵旋转)

    48. Rotate Image     Total Accepted: 69437 Total Submissions: 198781 Difficulty: Medium You are give ...

  7. [leetcode]Rotate List @ Python

    原题地址:https://oj.leetcode.com/problems/rotate-list/ 题意: Given a list, rotate the list to the right by ...

  8. [leetcode]Rotate Image @ Python

    原题地址:https://oj.leetcode.com/problems/rotate-image/ 题意: You are given an n x n 2D matrix representin ...

  9. 2016.5.16——leetcode:Rotate Array,Factorial Trailing Zeroe

    Rotate Array 本题目收获: 题目: Rotate an array of n elements to the right by k steps. For example, with n = ...

随机推荐

  1. 网络采集软件核心技术剖析系列(3)---如何使用C#语言下载博文中的全部图片到本地并可以离线浏览

    一 本系列随笔概览及产生的背景 本系列开篇受到大家的热烈欢迎,这对博主是莫大的鼓励,此为本系列第三篇,希望大家继续支持,为我继续写作提供动力. 自己开发的豆约翰博客备份专家软件工具问世3年多以来,深受 ...

  2. debian6 安装VirtualBox的方法

    方法一: 参考: https://www.virtualbox.org/wiki/Linux_Downloads    更新sources.list deb http://download.virtu ...

  3. WebApi单元测试记录

    一.MessageHandler不一定是全局的,也可以只应用到指定的Router上 .定义一个handler // Pipelines HttpMessageHandler affiliateShip ...

  4. SpringBoot整合Quartz定时任务

    记录一个SpringBoot 整合 Quartz 的Demo实例 POM.XML文件 <!-- 定时器任务 quartz需要导入的坐标 --> <dependency> < ...

  5. android应用开发-从设计到实现 2-8 组件与经常使用模式

    组件与经常使用模式 前面已经比較全面的介绍了Material Design相关的设计哲学.设计原理和方法论. 这一章開始,我们将看看这些原理是怎样在安卓系统其中得到实践的. 一个应用并非全然从什么都没 ...

  6. Java笔记5:单例模式

    一.应用杨景 在计算机系统中,线程池.缓存.日志对象.对话框.打印机.显卡的驱动程序对象常被设计成单例.这些应用都或多或少具有资源管理器的功能.每台计算机可以有若干个打印机,但只能有一个Printer ...

  7. 大话JS神器之Promise

    前段时间的工作中,由于项目要在前端实现存储,于是便使用了websql,而websql的API涉及到了很多的异步问题,如果采取回调函数的方式处理,代码不够优雅,而且不利于理解,于是便找到了Promise ...

  8. eclipse 配置maven 项目tomcat 运行

  9. 阿里云ECS linux通过rinetd 端口转发来访问内网服务

    一.场景说明: 可以通过端口映射的方式,来通过具有公网的云服务器 ECS 访问用户名下其它未购买公网带宽的内网 ECS 上的服务.端口映射的方案有很多,比如 Linux 下的 SSH Tunnel.r ...

  10. 三位一体的漏洞分析方法-web应用安全测试方法

    本文转自乌云知识库 0x00 前言 节选自: http://www.owasp.org.cn/OWASP_Conference/owasp-20140924/02OWASPWeb20140915.pd ...