My Blog List
[Leetcode Solution] Rotate List
/**
* 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 || k==0)return head;
ListNode*p=head,*q=p->next;
int len=1;
while(q!=NULL){
p=p->next;
q=q->next;
len++;
}
p->next=head;
p=head;
for(int i=0;inext;
q=p->next;
p->next=NULL;
return q;
}
};
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Enter you comment