【LeetCode】 Rotate List 循环链表
题目: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 循环链表的更多相关文章
- C++ STL@ list 应用 (leetcode: Rotate Array)
STL中的list就是一双向链表,可高效地进行插入删除元素. List 是 C++标准程式库 中的一个 类 ,可以简单视之为双向 连结串行 ,以线性列的方式管理物件集合.list 的特色是在集合的任何 ...
- [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 ...
- [LeetCode] Rotate List 旋转链表
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
- [LeetCode] Rotate Image 旋转图像
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- LeetCode——Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given ...
- [LeetCode]Rotate Image(矩阵旋转)
48. Rotate Image Total Accepted: 69437 Total Submissions: 198781 Difficulty: Medium You are give ...
- [leetcode]Rotate List @ Python
原题地址:https://oj.leetcode.com/problems/rotate-list/ 题意: Given a list, rotate the list to the right by ...
- [leetcode]Rotate Image @ Python
原题地址:https://oj.leetcode.com/problems/rotate-image/ 题意: You are given an n x n 2D matrix representin ...
- 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 = ...
随机推荐
- ProgressDialog进度条对话框
(一) 1.效果图: 2.activity_main.xml <?xml version="1.0" encoding="utf-8"?> < ...
- Ubuntu 16.04屏幕阅读Screen Reader导致快捷键失灵的问题解决
开启和关闭快捷键:[Alt]+[Win]+[S] 如果关了之后开机还自动启动时,那么直接把它卸载: sudo apt-get remove gnome-orca killall orca 参考: ht ...
- ThinkPHP模板中JS等带花括号处会被解析错误的解决办法
如下图,当本人在ThinkPHP框架的模板中写jQuery代码的时候,写了一些注释,并且注重是斜线和换括号{是连着一起的,这层语法上来时是没问题的,但是在ThinkPHP 的模板引擎解析下,会被解析掉 ...
- Nginx 编译参数详解/大全
Nginx参数: –prefix= 指向安装目录 –sbin-path 指向(执行)程序文件(nginx) –conf-path= 指向配置文件(nginx.conf) –error-log-path ...
- WebStorm中部署网页到Tomcat
1. 新建一个Deployment 在上面过程中Add Server弹窗下如下输入 2. 配置Deployment的Connection选项卡 3. 配置Deployment的Mappings选项卡 ...
- javascript(JQuery)元素操作
html代码如下: <div id="picK"> <ul> <li style="float:left;width:90px;" ...
- Server 2008 R2 安装活动目录和配置DNS及故障解决
一.安装windows2008_64bit_r2 给服务器设置密码:xxxxxxxx 二.安装配置DNS规则 1.更改服务器主机名:DCserver 2.配置静态IP地址 IP地址:10.0.100. ...
- php核心技术与最佳实践知识点(上)
一.基础 1.serialize:序列化一个类,只是保存了类的属性,所以还需要反序列化unserialize的时候包含该类. 2.对于将array转为object,这个转换因为没有具体的类,所以称为了 ...
- Fedora20上Xen的安装与部署
XEN 是一种直接执行在硬件上一层软件,它能够让电脑硬件上同一时候跑多个用户的操作系统.其体系结构例如以下: XEN Hypervisor :介于操作系统和硬件之间的一个软件描写叙述层.它负责在各个虚 ...
- Subl 命令
Subl 是sublime 的命令 添加环境变量后可以,在cmd 或者git 下直接 使用subl 进行 打开sublime Example: Subl 打开编辑器 Subl . 将当 ...