62- circle last remaining digital

Topic: 0,1, ..., n-1 of the n numbers arranged in a circle, beginning with the number 0, each time removing the first m digits from inside the circle. Seeking the circle where the last remaining digit.

def delete_circle_n(head,n):
    p = head
    while p!=p.next:
        i = 0
        while i<n:
            p = p.next
            i += 1
        q = p.next
        p.data = q.data
        p.next = q.next
        del q
    return p

  NOTE: Josephus problem, a circular linked list node simulation obtained using the last remaining, i.e., the node p == p.next.

Guess you like

Origin www.cnblogs.com/kingshine007/p/11578571.html