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 ...
随机推荐
- 打印cell的视图层次结构
#ifdef DEBUG NSLog(@"Cell recursive description:\n\n%@\n\n", [cell performSelector:@select ...
- webservice配置
服务端配置:第一步:引用jar包commons-httpclient.jarcommons-logging.jarjdom-10.jarwsdl4j-1.6.1.jarxbean-spring-2.8 ...
- 45.Android 第三方开源库收集整理(转)
原文地址:http://blog.csdn.net/caoyouxing/article/details/42418591 Android开源库 自己一直很喜欢Android开发,就如博客签名一样, ...
- Intent和Intent Filter Context
http://www.android-doc.com/reference/android/content/Intent.html An intent is an abstract descriptio ...
- MyEclipse------黑科技
自动计算器(+,-,*,/) <form method="post" oninput="o.value = parseInt(a.value) + parseInt ...
- PHP中PDO的配置与说明
住[PDO是啥] PDO是PHP5新加入的一个重大功能,因为在PHP5以前的php4/php3都是一堆的数据库扩展来跟各个数据库的连接和处理,什么php_mysql.dll.php_pgsql.dll ...
- mybatis使用小记
参考资料:http://blog.csdn.net/hupanfeng/article/details/9098453 1.设置不缓存每次查询的结果: 如题,通过设置 flushCache=" ...
- VS自带WCF测试客户端简单介绍
在目前的二次开发项目中,一些信息是放在客户那里的,只给你一个服务地址,不知道具体有什么方法,每次想调用一个服务不知道能不能实现目前的需求,只能测试.写个测试程序真的划不来,占用时间不说,而且你忙了一上 ...
- lnmp一键安装包删除添加的域名
lnmp一键安装包删除添加的域名 如果使用lnmp一键安装包/root/vhost.sh 添加的域名可以,可以删除/usr/local/nginx/conf/vhost/要删除的域名.conf 文件, ...
- Windows下安装配置SubVersion的简明步骤
[使用的安装程序和文档说明] svn-1.4.0-setup.exe:Subversion服务端1.4.0安装程序: SubService.rar: SubServe服务Windows辅助工具; T ...