题目: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. iOS键盘监听事件

    1.注册键盘通知事件 NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; // 键盘将出现事件监听 [center ...

  2. 【转载】linux2.6内核initrd机制解析

    题记 很久之前就分析过这部分内容,但是那个时候不够深入,姑且知道这么个东西存在,到底怎么用,来龙去脉咋回事就不知道了.前段时间工作上遇到了一个initrd的问题,没办法只能再去研究研究,还好,有点眉目 ...

  3. kubernetes环境下私有仓库搭建

    前期在客户那里搭建了基本运行环境,鉴于很多企业的环境都是内部网无法连接外部,因此搭建私有仓库是逃避不开的问题,按照网上的步骤搭建,虽然遇到一些问题,但还好都算容易解决了,下面大致把步骤记录一下便于下次 ...

  4. http://blog.163.com/eugeneheen_chen@126/blog/static/120812157201291994916866/

    http://blog.163.com/eugeneheen_chen@126/blog/static/120812157201291994916866/

  5. Android画图系列(二)——自己定义View绘制基本图形

    这个系列主要是介绍下Android自己定义View和Android画图机制.自己能力有限.假设在介绍过程中有什么错误.欢迎指正 前言 在上一篇Android画图系列(一)--自己定义View基础中我们 ...

  6. web.xml文件中配置mime下载文件类型(转)

    转自:http://5aijava.iteye.com/blog/166600 TOMCAT在默认情况下下载.rar的文件是把文件当作text打开,以至于IE打开RAR文件为乱码,如果遇到这种情况时不 ...

  7. 学习ajax总结

    之前公司的ajax学习分享,做一点总结,加深记忆 什么是ajax? 异步的的js和xml,用js异步形式操作xml,工作主要是数据交互 借阅用户操作时间,减少数据请求,可以无刷新请求数据 创建一个对象 ...

  8. Python Socket API 笔记

    将上节中的C#该成Python版的容易程度大大超出了我的意料之外.从来没有发现,仅仅用灰尘简单的几句话就实现了该程序的主要功能,可见python的简易和强大之处.这里先对SocketAPI 做一下总结 ...

  9. lodash 工具库

    lodash是一套工具库,内部封装了很多字符串.数组.对象等常见数据类型的处理函数. 1.lodash的引用 import _ from 'lodash' 用一个数组遍历来说明为什么要使用lodash ...

  10. Android-RelativeLayout布局技巧(一)

    如果有一个需求是这样的,在标题中的右上角有一个button <?xml version="1.0" encoding="utf-8"?> <L ...