Rotate an array and a linked list
Page 1 of 1 • Share •
Rotate an array and a linked list
Suppose we have an array A having n elements. You have to rotate the array by k positions.
For e.g. if we have the array A={1,2,3,4,5} and k=2 then the resulting array should be {3,4,5,1,2}. Try to post an in-place solution. The array need not be sorted originally. The question has multiple solutions. Try to discuss all.
Now what will be the approach if you want to rotate a singly linked list instead of an array.
For e.g. if we have the array A={1,2,3,4,5} and k=2 then the resulting array should be {3,4,5,1,2}. Try to post an in-place solution. The array need not be sorted originally. The question has multiple solutions. Try to discuss all.
Now what will be the approach if you want to rotate a singly linked list instead of an array.
Admin- Admin
- Posts: 39
Join date: 2008-08-03

Re: Rotate an array and a linked list
# include<iostream>
using namespace std;
main()
{int i,n,k;
cout<<"enter size of array";
cin>>n;
int a[n];
for(i=0;i<n;++i)
cin>>a[i];
cout<<"\nenter k";
cin>>k;
k=k%n;
for(i=0;i<n;++i)
{if(i+k<n)
cout<<a[i+k]<<" ";
else
cout<<a[i+k-n]<<" ";
}
// system("pause");
}
using namespace std;
main()
{int i,n,k;
cout<<"enter size of array";
cin>>n;
int a[n];
for(i=0;i<n;++i)
cin>>a[i];
cout<<"\nenter k";
cin>>k;
k=k%n;
for(i=0;i<n;++i)
{if(i+k<n)
cout<<a[i+k]<<" ";
else
cout<<a[i+k-n]<<" ";
}
// system("pause");
}
mnnit.rahul- Posts: 16
Join date: 2008-08-10
mnnit.rahul
You dont have to print the answer ...
you have to change the original array ..
you have to change the original array ..
bhagalpur- Posts: 3
Join date: 2008-08-03
Re: Rotate an array and a linked list
printf("enter the size and k");
scanf("%d%d",&n,&k);
for(j=0;j<k;j++)
{
c=a[0];
for(i=0;i<n-1;i++)
a[i]=a[i+1];
a[n-1]=c;
}
now print array a[];
scanf("%d%d",&n,&k);
for(j=0;j<k;j++)
{
c=a[0];
for(i=0;i<n-1;i++)
a[i]=a[i+1];
a[n-1]=c;
}
now print array a[];
sweetesh singh- Posts: 8
Join date: 2008-08-12
Re: Rotate an array and a linked list
for rotating the list , first break the list at pont k(measured from front),then attach it to the last point.point of attachment should be tail->head.
sweetesh singh- Posts: 8
Join date: 2008-08-12
rotating the list.......
this should work...
1->get to the kth element from the last
2->point this element to the head
3->reverse the list beyond k
1->get to the kth element from the last
2->point this element to the head
3->reverse the list beyond k
prani- Posts: 15
Join date: 2008-08-12
Re: Rotate an array and a linked list
tiwari ji
please explain the working by taking an example, i am unable to get it
please explain the working by taking an example, i am unable to get it
sweetesh singh- Posts: 8
Join date: 2008-08-12
rotate a singly linked list
void rotate(int n)
{
node *r;
r=start;
while(r->next!=NULL)
{
r=r->next;
}
while(n--)
{
r->next=start;
r=start;
start=start->next;
cout<<r->n<<endl;
r->next=NULL;
}
}
{
node *r;
r=start;
while(r->next!=NULL)
{
r=r->next;
}
while(n--)
{
r->next=start;
r=start;
start=start->next;
cout<<r->n<<endl;
r->next=NULL;
}
}
harit- Posts: 2
Join date: 2008-09-09
Permissions of this forum:
You cannot reply to topics in this forum





