Find last 2 survivors in N persons standing in a circle after killing next to immediate neighbour | Node for a Linked List ; Function to find the last 2 survivors ; Total is the count of alive people ; Initiating the list of n people ; Total != 2 is terminating condition because at last only two - person will remain alive ; de represent next person to be deleted or killed ; Last two person to survive ( in any order ) ; Driver code
class newNode : NEW_LINE INDENT def __init__ ( self , val ) : NEW_LINE INDENT self . val = val NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def getLastTwoPerson ( n ) : NEW_LINE INDENT total = n NEW_LINE head = newNode ( 1 ) NEW_LINE temp = head NEW_LINE for i in range ( 2 , n + 1 , 1 ) : NEW_LINE INDENT temp . next = newNode ( i ) NEW_LINE temp = temp . next NEW_LINE DEDENT temp . next = head NEW_LINE temp = head NEW_LINE de = None NEW_LINE while ( total != 2 ) : NEW_LINE INDENT de = temp . next . next NEW_LINE temp . next . next = temp . next . next . next NEW_LINE temp = temp . next NEW_LINE del de NEW_LINE total -= 1 NEW_LINE DEDENT print ( temp . val , temp . next . val ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 2 NEW_LINE getLastTwoPerson ( n ) NEW_LINE DEDENT