链表-环形单链表的约瑟夫问题

//环形单链表的约瑟夫问题
public class Node{
    public int value;
    public Node next;
    public Node(int data){
        this.data=data;
    }
}
public Node josephusKill(Node head,int m){
    if(head==null||head.next==head||m<1){
        return head;
    }
    Node last=head;
    while(last.new!=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;
}

猜你喜欢

转载自blog.csdn.net/weixin_42146769/article/details/88386885