61. Rotate List
来源:互联网 发布:美国大律师 知乎 编辑:程序博客网 时间:2024/04/30 07:39
Given a list, rotate the list to the right by k places, where k is non-negative.
For example:
Given 1->2->3->4->5->NULL
and k = 2
,
return 4->5->1->2->3->NULL
.
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: int getLen(ListNode* head) { int len = 0; while(head) { len++; head = head->next; } return len; } ListNode* rotateRight(ListNode* head, int k) { if(head==NULL) return NULL; ListNode* fast = head; int i; int len = getLen(head); k = k % len; if(k==0) return head; int n = len - k; for(i=1; i<n; i++) { fast = fast->next; } ListNode *head2 = fast->next; fast->next = NULL; ListNode* slow = head2; for(int j = 1; j <k; j++) { slow = slow->next; } slow->next = head; return head2; }};
0 0
- [LeetCode]61.Rotate List
- 61. Rotate List
- LeetCode --- 61. Rotate List
- [Leetcode] 61. Rotate List
- 61. Rotate List
- [leetcode] 61.Rotate List
- [leetcode] 61. Rotate List
- 61. Rotate List
- leetcode 61. Rotate List
- LeetCode 61. Rotate List
- [leetcode] 61. Rotate List
- 61. Rotate List
- 61. Rotate List
- 61. Rotate List LeetCode
- leetcode 61. Rotate List
- 61. Rotate List
- 61. Rotate List
- 61. Rotate List
- ios开发总结之UIImageView常用属性
- 如何使 类的成员函数作为回调函数
- JavaScript与OC的相互调用
- 使用UIWebView时objective-c与javascript互相调用
- SiteWhere用户使用手册
- 61. Rotate List
- [剑指offer]复杂链表的复制
- 关于建立时间和保持时间
- 你真的了解Android Notification吗?
- PAT-B 1019. 数字黑洞
- vSphere Client 6.0 远程端口发布
- CoreData的使用以及coreData中的多线程问题(一)
- Telnet 相关知识解析
- linux BASH_SOURCE[0]