-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathArray+ListRotation.cpp
More file actions
43 lines (41 loc) · 918 Bytes
/
Array+ListRotation.cpp
File metadata and controls
43 lines (41 loc) · 918 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class Solution {
public:
void rotate(vector<int>& nums, int k) {
vector<int>copy=nums;
int i,sz=nums.size();
for(i=0;i<sz;i++)
copy[(i+k)%sz]=nums[i];
nums=copy;
}
};
void rotate(vector<int>& nums, int k) {
int sz=nums.size();
k=k%sz;
reverse(nums.begin(),nums.end()-k);
reverse(nums.end()-k,nums.end());
reverse(nums.begin(),nums.end());
}
class Solution {
public:
ListNode* rotateRight(ListNode* head, int k)
{
if((head==NULL) || (head->next==NULL))
return head;
if(k==0)
return head;
int len=1,con,i;
ListNode *x=head , *h1;
while(x->next!=NULL)
{
x=x->next;
len++;
}
x->next=head;
k=k%len;
for(i=0;i<len-k;i++)
x=x->next;
h1=x->next;
x->next=NULL;
return h1;
}
};