-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrotate.java
More file actions
74 lines (73 loc) · 1.07 KB
/
rotate.java
File metadata and controls
74 lines (73 loc) · 1.07 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package practice;
import java.util.*;
public class rotate
{
static class Node
{
Node next;
int data;
Node(int data)
{
this.data=data;
next=null;
}
}
static Node head;
void print(Node head)
{
Node cur=head;
while(cur!=null)
{
System.out.print(cur.data+" ");
cur=cur.next;
}
}
void insert(int data)
{
Node temp=new Node(data);
if(head==null)
{
head=temp;
return ;
}
Node cur=head;
while(cur.next!=null)
{
cur=cur.next;
}
cur.next=temp;
}
void rotate(int r)
{
Node cur=head;
int count=1;
while(count<r&&cur.next!=null)
{
cur=cur.next;
count++;
}
Node temp=cur;
while(cur.next!=null)
{
cur=cur.next;
}
cur.next=head;
head=temp.next;
temp.next=null;
}
public static void main(String args[])
{
rotate r=new rotate();
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size");
int size=sc.nextInt();
for(int i=0;i<size;i++)
{
r.insert(sc.nextInt());
}
System.out.println("Enter the place to roatate");
int r1=sc.nextInt();
r.rotate(r1);
r.print(r.head);
}
}