Rotate List
2014-07-07 08:28
393 查看
/** * 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) { return null; } ListNode slow = head, fast = head; for(int i = 0; i < n; i++) { fast = fast.next; if(fast == null) { fast = head; } } while(fast.next != null) { slow = slow.next; fast = fast.next; } fast.next = head; head = slow.next; slow.next = null; return head; } }
Time: O(n)
Space: O(1)
相关文章推荐
- [LeetCode]Rotate List
- leetcode_061 Rotate List
- Rotate List
- LeetCode 61. Rotate List
- leetcode第一刷_Rotate List
- LintCode 170. Rotate List
- [Leetcode] Rotate List
- LeetCode 61: Rotate List
- Rotate List
- 【LeetCode】Rotate List
- Leetcode | Rotate List
- Leetcode NO.61 Rotate List
- [leetcode][list] Rotate List
- Rotate List
- [Leetcode]Rotate List
- 61. Rotate List
- Leetcode 61 Rotate List
- leetcode 61. Rotate List
- LeetCode 61. Rotate List
- Leetcode 61. Rotate List