您的位置:首页 > 其它

61. Rotate List

2017-02-02 03:55 106 查看
题意简单,但是但是!!!!

超级多小细节,一定要二刷!!!!

练手好体!!!

/**
* 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) {
int n = 1;
if(head == NULL || head -> next == NULL) return head;
ListNode* t;
ListNode* u;
ListNode* newhead;
for(t = head; t -> next != NULL; t = t -> next)
n++;
if(n == k) return head;
k = k % n;
if(k == 0) return head;
t = head;

for(int i = 1; i < n - k; ++ i)
t = t -> next;
u = t -> next;
newhead = u;
t -> next = NULL;
while(u -> next != NULL)
u = u -> next;
u -> next = head;
return newhead;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: