LeetCode 60 Rotate List
2014-06-21 00:19
232 查看
Given a list, rotate the list to the right by k places, where k is non-negative.
For example:
Given
return
For example:
Given
1->2->3->4->5->NULLand k =
2,
return
4->5->1->2->3->NULL.
public class Solution { public ListNode rotateRight(ListNode head, int n) { if (n < 1 || head == null) return head; ListNode dumny = new ListNode(-1); ListNode p = head; int counter = 1; while (p.next != null) { p = p.next; counter++; } p.next = head; int i = 1; p = head; while (i < (counter - n % counter)) { p = p.next; i++; } dumny.next = p.next; p.next = null; return dumny.next; } }
相关文章推荐
- LeetCode(60)RotateList
- leetcode[60] Rotate List
- 【LeetCode】Rotate List
- 【Leetcode】Rotate List
- leetcode_c++:链表:Rotate List(061)
- LeetCode Rotate List
- 个人记录-LeetCode 61. Rotate List
- Leetcode Rotate List 旋转链表
- [leetcode]61. Rotate List@Java解题报告
- 29_leetcode_Rotate List
- [LeetCode] 61. Rotate List
- leetcode之Rotate List
- #leetcode#Rotate List
- [Leetcode]#61 Rotate List
- 【LeetCode-面试算法经典-Java实现】【061-Rotate List(旋转单链表)】
- [leetcode-61]Rotate List(c)
- Rotate List leetcode java
- rotate List|Leetcode c++
- [leetcode]Rotate List
- [LeetCode] Rotate List