62- the last remaining number in the circle-python

Question: 0,1,...,n-1 these n numbers are arranged in a circle, starting from the number 0, and deleting the mth number from this circle each time. Find the last number remaining in the circle.

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: For the Joseph ring problem, use a circular linked list to simulate the last remaining node, that is, the node when p==p.next.

Guess you like

Origin blog.csdn.net/wh672843916/article/details/105503769