LeetCode Solutions : Rotate List
2014-11-13 19:17
381 查看
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.
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { public ListNode rotateRight(ListNode head, int n) { if(head==null||head.next==null) return head; ListNode rear=head; int len=1; while(rear.next!=null){ rear=rear.next; len++; } rear.next=head; int breakPoint=len-n%len; rear=head; int count=1; while(count<breakPoint){ rear=rear.next; count++; } head=rear.next; rear.next=null; return head; } }
相关文章推荐
- LeetCode 61. Rotate List
- 【LeetCode】Rotate List
- [LeetCode] 61. Rotate List 旋转链表
- Leetcode Problem.—Rotate List C++实现
- Rotate List -- LeetCode
- [Leetcode 61, Medium] Rotate List
- LeetCode 61. Rotate List
- 【leetcode】Rotate List
- leetcode之Rotate List
- Leetcode rotate list
- LeetCode 61. Rotate List2.
- 【LeetCode】61. Rotate List解法及分析
- [leetcode] Rotate List
- LeetCode--Rotate List
- 【LeetCode】Rotate List
- leetcode 61. Rotate List
- leetcode:Rotate List 【Java】
- leetcode做题总结,题目Rotate List 2012/03/27
- LeetCode 61. Rotate List(旋转链表)
- 【leetcode】61. Rotate List【java】