约瑟夫环问题:一圈共有N个人,开始报数,报到M的人自杀,然后重新开始报数,问最后自杀的人是谁?
//链表实现
public class Node{
public int value;
public Node next;
public Node(int data){
this.value=data;
}
}
public Node josephuskill(Node head,int m){
if(head==null||head.next==head||m<1){
return head;
}
Node last=head;
while(last.next!=head){
last=last.next;
}
int count=0;
while(head!=last){
if(++count==m){
last.next=head.next;
count=0;
}
else{
last=last.next;
}
head=last.next;
}
return head;
}