Rotate List —— LeetCode
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有可能比链表的长度还长,比如长度为3的链表,k=2000000000,所以开始需要遍历一下链表算出长度,k%=len,然后就是两个pointer一个先走k步,另一个再走,最后把末尾的next节点设为head,新head就是慢指针指向的节点。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode rotateRight(ListNode head, int k) {
if(head == null||k == 0){
return head;
}
int fast = k;
ListNode fastNode = head;
ListNode slowNode = head;
ListNode pre = slowNode;
int len = 0;
while(fastNode!=null){
len++;
fastNode=fastNode.next;
}
fast %= len;
fastNode = head;
if(fast == 0){
return head;
}
while(fastNode!=null&&fast-->0){
fastNode = fastNode.next;
}
while(fastNode!=null){
fastNode = fastNode.next;
pre = slowNode;
slowNode = slowNode.next;
}
ListNode newHead = new ListNode(0);
newHead.next = slowNode;
pre.next = null;
while(slowNode.next!=null){
slowNode = slowNode.next;
}
slowNode.next = head;
return newHead.next;
}
}
Rotate List —— LeetCode的更多相关文章
- 48. Rotate Image - LeetCode
Question 48. Rotate Image Solution 把这个二维数组(矩阵)看成一个一个环,循环每个环,循环每条边,每个边上的点进行旋转 public void rotate(int[ ...
- Rotate Image [LeetCode]
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- Rotate Array leetcode
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...
- Rotate Image leetcode java
题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwis ...
- Rotate List leetcode java
题目: Given a list, rotate the list to the right by k places, where k is non-negative. For example: Gi ...
- 189. Rotate Array - LeetCode
Question 189. Rotate Array Solution 题目大意:数组中最后一个元素移到第一个,称动k次 思路:用笨方法,再复制一个数组 Java实现: public void rot ...
- 796. Rotate String - LeetCode
Question 796. Rotate String Solution 题目大意:两个字符串匹配 思路:Brute Force Java实现: public boolean rotateString ...
- Rotate List || LeetCode
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * } ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
随机推荐
- (转)smarty实现多级分类的方法
--http://www.aspku.com/kaifa/php/44679.html 这篇文章主要介绍了smarty实现多级分类的方法,涉及循环读取的技巧,非常具有实用价值,需要的朋友可以参考下 ...
- Android WebView和JavaScript交互
JavaScript在现在的网页设计中用得很多,Android 的WebView可以载入网页,WebView也设计了与JavaScript通信的桥梁.这篇主要介绍一下WebViewk控件如何和Java ...
- 内网映射到公网工具 --- ngrok
ngrok可以将内网映射到公网上,这样就可以在公网上访问你的网络服务. 该工具通常在进行app开发和微信开发时比较有用,这样就可避免在公网服务器上单独部署项目,通过映射,直接连接本地服务即可进行开发. ...
- C语言字符串库函数的实现
1.strlen(字符串的长度) size_t Strlen(const char* str) { assert(str); ;; ++i) { if (str[i] == '\0') return ...
- Xcode 使用自定义字体
添加对应的字体(.ttf或.odf)到工程的resurce,使用cocos2d中的FontLabel库,FontLabel继承于UILabel,象UILabel一样使用就好了 fontName直接使用 ...
- 关于使用iframe标签自适应高度的使用
在ifrome内设定最小高度,(此方法只适用于页面内切换高度不一.但是会保留最大高度,返回后保持最大高度不再回到最初页面的高度) <iframe id="one4" widt ...
- spring mvc easyui tree 异步加载树
使用spring mvc 注解 异步加载一棵树 jsp: <ul id="orgInfoTree"></ul> $(function(){ loadOrgT ...
- Java初试
另外在Java语言的代码内部书写文件路径时,需要注意大小写,大小写需要保持一致,路径中的文件夹名称区分大小写.由于’\’是Java语言中的特殊字符,所以在代码内部书写文件路径时,例如代表“c:\tes ...
- Python核心编程2第一章课后练习
1-1 在windows下的安装方法在网上下载python2.7直接安装到C盘1)在系统变量中找到path. 2)编辑path值,添加你安装的python路径,C:\Python27. 3)检验pyt ...
- C++实现红黑树,仿STL封装
//RB_Tree.hpp //The code of red black trees //2011/12/31 by Adoo // The foundation :http://www.roadi ...