class LinkListRotateAntiClock {
Node head;
class Node{
int data;
Node next;
Node(int data){
this.data=data;
this.next=null;
}
@Override
public String toString() {
return "{"+this.data+"}";
}
}
public void push(int data){
Node node=new Node(data);
if(head==null){
head=node;
}else{
Node current=head;
while(current.next!=null){
current=current.next;
}
current.next=node;
}
}
public Node rotateAntiClockWise(int k, Node node){
if(node ==null || k<0){
return node;
}
int sizeOfLinkList=getSizeOfList(node);
k=k%sizeOfLinkList;
if(k==0){
return node;
}
Node tmp=node;
int i=1;
while(i < k){
tmp=tmp.next;
i++;
}
Node temp=tmp.next;
Node head=temp;
tmp.next=null;
while(temp.next!=null){
temp=temp.next;
}
temp.next=node;
return head;
}
public int getSizeOfList(Node node){
if(node ==null){
return 0;
}
return getSizeOfList(node.next)+1;
}
public void display(Node head){
if(head==null){
return;
}else{
Node current=head;
while(current!=null){
System.out.print(current);
current=current.next;
}
System.out.println("********");
}
}
}
public class LinkListAntiClockwise {
public static void main(String[] args) {
LinkListRotateAntiClock linkListRotateAntiClock =new LinkListRotateAntiClock();
linkListRotateAntiClock.push(1);
linkListRotateAntiClock.push(2);
linkListRotateAntiClock.push(3);
linkListRotateAntiClock.push(4);
linkListRotateAntiClock.push(5);
linkListRotateAntiClock.push(5);
linkListRotateAntiClock.display(linkListRotateAntiClock.head);
LinkListRotateAntiClock.Node node = linkListRotateAntiClock.rotateAntiClockWise(2, linkListRotateAntiClock.head);
linkListRotateAntiClock.display(node);
}
}
TechInsiderStory is place where you can find basic knowledge of various technology and framework. TechInsiderStory is provide free knowledge on various technology and framework.
LinkList Rotate AntiClockWise
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment