Lucky alive person in a circle | Code Solution to sword puzzle | Node structure ; Function to find the luckiest person ; Create a single node circular linked list . ; Starting from first soldier . ; condition for evaluating the existence of single soldier who is not killed . ; deleting soldier from the circular list who is killed in the fight . ; Returning the Luckiest soldier who remains alive . ; Driver code
class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def newNode ( data ) : NEW_LINE INDENT node = Node ( data ) NEW_LINE return node NEW_LINE DEDENT def alivesol ( Num ) : NEW_LINE INDENT if ( Num == 1 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT last = newNode ( 1 ) ; NEW_LINE last . next = last ; NEW_LINE for i in range ( 2 , Num + 1 ) : NEW_LINE INDENT temp = newNode ( i ) ; NEW_LINE temp . next = last . next ; NEW_LINE last . next = temp ; NEW_LINE last = temp ; NEW_LINE DEDENT curr = last . next ; NEW_LINE temp = None NEW_LINE while ( curr . next != curr ) : NEW_LINE INDENT temp = curr ; NEW_LINE curr = curr . next ; NEW_LINE temp . next = curr . next ; NEW_LINE del curr ; NEW_LINE temp = temp . next ; NEW_LINE curr = temp ; NEW_LINE DEDENT res = temp . data ; NEW_LINE del temp ; NEW_LINE return res ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 100 ; NEW_LINE print ( alivesol ( N ) ) NEW_LINE DEDENT