Java for LeetCode 061 Rotate List
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
.
解题思路:
只需找到对应的位置,然后指向head,接着把之前节点指向null即可,注意k可以取大于length的值,所以k%=length,JAVA实现如下:
public ListNode rotateRight(ListNode head, int k) {
if(head==null||head.next==null)
return head;
ListNode temp=head;
int length=1;
while(temp.next!=null){
temp=temp.next;
length++;
}
if(k==length)
return head;
temp.next=head;
temp=head;
for(int i=1;i<length-k;i++)
temp=temp.next;
head=temp.next;
temp.next=null;
return head;
}
Java for LeetCode 061 Rotate List的更多相关文章
- Java for LeetCode 189 Rotate Array
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...
- Java for LeetCode 048 Rotate Image
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- [LeetCode] 61. Rotate List 旋转链表
Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: I ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- Java for LeetCode 214 Shortest Palindrome
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- Java for LeetCode 212 Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- Java for LeetCode 211 Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...
- Java for LeetCode 210 Course Schedule II
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- Java for LeetCode 200 Number of Islands
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
随机推荐
- Emmet
一.简介 Emmet (前身为 Zen Coding) ,不是软件也不是代码,是编辑器(如sublime text)的插件,相应的后缀文件(.html/.css)输入指定的缩写语法,按下tab键就能生 ...
- 洛谷P2633 王后万岁
题目描述 byteland的王后深受百姓爱戴.为了表达他们的爱,国民们打算占领一个新的国家,并以王后的名字命名.这个国家有n座城市.城市之间有双向道路连接,且每两个城市之间有且仅有一条道路.每座城市对 ...
- css常用技巧
去点号 list-style:none; 字体居中 text-align:center; 链接去下划线 text-decoration:none; 鼠标禁止右键 <body oncontextm ...
- (原)String类两种实例化的区别
String有两种实例化方式,一种是通过直接赋值的方式,另外一种是使用标准的new调用构造方法完成实例化. public class StringDemo { public static void m ...
- POJ3259Wormholes(判断是否存在负回路)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 38300 Accepted: 14095 Descr ...
- JDK,JRE,JVM区别与联系(ZZ)
http://www.cnblogs.com/hencehong/p/3252166.html 我们开发的实际情况是:我们利用JDK(调用JAVA API)开发了属于我们自己的JAVA程序后,通过JD ...
- boost解析json
#include <QtCore/QCoreApplication> #include <boost/property_tree/ptree.hpp> #include < ...
- CSS 2D转换 matrix() 详解
2D转换 IE10.Firefox.Opera 支持 transform 属性 Chrome.Safari 需要前缀 -webkit- . IE9 需要前缀 -ms- . translate():接收 ...
- PHP文件包含漏洞剖析
一. 什么才是”远程文件包含漏洞”?回答是:服务器通过php的特性(函数)去包含任意文件时,由于要包含的这个文件来源过滤不严,从而可以去包含一个恶意文件,而我们可以构造这个恶意文件来达到邪恶的目的. ...
- 基于.NET的大型Web站点StackOverflow架构分析(转)
Stack Overflow网址:http://stackoverflow.com/ 当前访问量:每月9500PV(每天300多万PV) 当前Alexa排名:149 所用.NET技术:C#.Visua ...