【LeetCode】Rotate List
2014-12-28 16:27
218 查看
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode *rotateRight(ListNode *head, int k) { if (head == NULL) return NULL; if(k==0) return head; int len = 1; ListNode *tmp,*ptr; ListNode *newHead; tmp = head; ptr = head; while (tmp->next) { len++; tmp = tmp->next; } tmp->next = head; int step; step = len - k%len; for (int i = 1; i < step; i++) { ptr = ptr->next; } newHead = ptr->next; ptr->next = NULL; return newHead; } };
相关文章推荐
- LeetCode 61. Rotate List
- LeetCode-Rotate List
- leetcode--Rotate List
- 【Leetcode】【Medium】Rotate List
- LeetCode----- 61. Rotate List
- leetcode 61. Rotate List
- [LeetCode] Rotate List
- 【leetcode每日一题】61.Rotate List
- Leetcode 61. Rotate List
- leetcode第一刷_Rotate List
- [leetcode] Rotate List
- Rotate List [leetcode] 的两个思路
- leetcode:Rotate List
- leetcode_061 Rotate List
- [LeetCode] Rotate List
- 《leetCode》:Rotate List
- LeetCode | Rotate List
- leetcode - 61.Rotate List
- [Leetcode]@python 61. Rotate List
- LeetCode-Rotate List