leetcode - Rotate List
2013-11-25 22:07
253 查看
class Solution { public: ListNode *rotateRight(ListNode *head, int k) { // Note: The Solution object is instantiated only once and is reused by each test case. if (head == NULL || head->next ==NULL) return head; int size = 0; ListNode * tmp = head; while (tmp!=NULL){ size++; tmp = tmp->next; } k = k%size; if (k<=0) return head; ListNode * rhead = NULL, * ltail = NULL, * rtail = NULL; ListNode * itr = head; int i = 0; while (itr !=NULL){ i++; if (i==k){ ltail = NULL; rhead = head; } else if (i>k){ ltail = rhead; rhead = rhead->next; } rtail = itr; itr = itr->next; } if (ltail!=NULL &<ail !=rtail) ltail->next = NULL; if (rhead!=NULL && rhead!=head){ rtail->next = head; return rhead; } return head; } };
相关文章推荐
- 61. Rotate List LeetCode
- leetcode-Rotate List (2014.3.10)
- [leetcode]Rotate List @ Python
- [LeetCode] 61. Rotate List 旋转链表
- leetcode 61. Rotate List
- leetcode---rotate-list---链表
- Leetcode Rotate List
- [LeetCode] Rotate List
- [LeetCode]Rotate List@python
- Leetcode Rotate List 旋转链表
- LeetCode题解:Rotate List
- LeetCode:Rotate List
- [leetcode] Rotate List
- LeetCode Rotate List
- Rotate List leetcode java
- leetcode 61. Rotate List
- leetcode 刷题之路 13 Rotate List
- 【Leetcode】61. Rotate List - 循环链表
- [LeetCode] Rotate List
- Leetcode61 rotate list