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 ...
随机推荐
- Cocos2d-X3.0 刨根问底(三)----- Director类源码分析
上一章我们完整的跟了一遍HelloWorld的源码,了解了Cocos2d-x的启动流程.其中Director这个类贯穿了整个Application程序,这章随小鱼一起把这个类分析透彻. 小鱼的阅读源码 ...
- BZOJ-2929 洞穴攀岩 最大流Dinic(傻逼题)
竟然没有1A真羞耻...1分钟不到读完题,10分钟不到打完....MD没仔细看...WA了一遍,贱! 2929: [Poi1999]洞穴攀行 Time Limit: 1 Sec Memory Limi ...
- BZOJ-1433 假期的宿舍 最大流+基础建图
网络流练习ing.. 1433: [ZJOI2009]假期的宿舍 Time Limit: 10 Sec Memory Limit: 162 MB Submit: 1748 Solved: 765 [S ...
- codevs1500 后缀排序
题目描述 Description 天凯是MIT的新生.Prof. HandsomeG给了他一个长度为n的由小写字母构成的字符串,要求他把该字符串的n个后缀(suffix)从小到大排序. 何谓后缀?假设 ...
- 网络包处理工具NetBee
What is NetBee? NetBee is a new library intended for several types of packet processing, such as pac ...
- JAVA中的数组是对象吗?
public class Main{ public static void main(String[] args) { int a[]={1,9}; //Object obj=new int[10]; ...
- DLUTOJ 1331 Maximum Sum
传送门 Time Limit: 1 Sec Memory Limit: 128 MB Description You are given an array of size N and anothe ...
- json格式不对引起的报错
报JSONDecondeError这种类型的错误的时候就要检查下json格式是否是正确的了,这里提供一个http://www.bejson.com/ Traceback (most recent ca ...
- 裴波那契数列 JavaScript 尾递归实现
一般递归实现 : //经典递归 function fibonacci(n) { return (function(n) { ) ; ); })(n); } 或者: function fibonacci ...
- 点击cell弹出一个日期选择器
- (void)setUpGroup2 { ILGroupItem *group = [[ILGroupItem alloc] init]; // 结束时间 ILSettingItem *endTim ...