题目: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. Inno Setup入门(一)——最简单的安装脚本

    地址:http://379910987.blog.163.com/blog/static/3352379720110238252326/ 一个最简单的安装脚本: 1.最简单的安装文件脚本: [setu ...

  2. Mysql的共享锁和排他锁(转载)

    mysql锁机制分为表级锁和行级锁,本文就和大家分享一下我对mysql中行级锁中的共享锁与排他锁进行分享交流. 共享锁又称为读锁,简称S锁,顾名思义,共享锁就是多个事务对于同一数据可以共享一把锁,都能 ...

  3. spring-data-jpa查询语句的书写实例小计

    //查询语句List<AuctionLot> alots = auctionLotRepository.findAllByAuctionIdAndAucIdIsNotNullAndIsOf ...

  4. git相关知识:如何避免某些文件无需提交

    查看所有命令 git help -a 查看所有概念解释 git help -g 某个命令的具体帮助信息 git help command 如何避免某些文件无需提交? 合作开发时个人的约定的不上传的文件 ...

  5. Android--使用XMLPull解析xml

    在Android中极力推荐的xmlpull方式解析xml.xmlpull不只能够使用在Android上.相同也适用于javase,但在javase环境下.你须要自己去获取xmlpull所依赖的类库. ...

  6. 【转载】游戏并发编程的讨论 & Nodejs并发性讨论 & 语法糖术语

    知乎上这篇文章对于游戏后端.性能并发.nodejs及scala等语言的讨论,很好,值得好好看. https://www.zhihu.com/question/21971645 经常了解一些牛逼技术人员 ...

  7. 算法导论-求x的n次方

    目录 1.分治求x的n次方思路 2.c++代码实现 内容 1.分治求x的n次方思路T(n)=Θ(lgn) 为了计算乘方数a^n,传统的做法(所谓的Naive algorithm)就是循环相乘n次,算法 ...

  8. [Spring boot] Configuring and Accessing a Data Source

    We need our data persistence with configuring our datasouce: In application.properties: spring.h2.co ...

  9. Servlet——简单用户登录实例+http协议解析

    编写项目.用户登录系统1.0版本号 登录界面Servlet: package com.gavin.view; import java.io.IOException; import java.io.Pr ...

  10. Pinterest架构:两年内月PV从零到百亿

    Pinterest正经历了指数级曲线般的增长,每隔一个半月就翻番.在这两年里,Pinterest,从 每月PV量0增长到100亿,从两名c创始人和一个工程师成长为四十个工程师,从一台MySQL 服务器 ...