Lucky alive person in a circle | Code Solution to sword puzzle | Java code to find the luckiest person ; 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 GFG { static class Node { int data ; Node next ; } ; static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . next = null ; return node ; } static int alivesol ( int Num ) { if ( Num == 1 ) return 1 ; Node last = newNode ( 1 ) ; last . next = last ; for ( int i = 2 ; i <= Num ; i ++ ) { Node temp = newNode ( i ) ; temp . next = last . next ; last . next = temp ; last = temp ; } Node curr = last . next ; Node temp = new Node ( ) ; while ( curr . next != curr ) { temp = curr ; curr = curr . next ; temp . next = curr . next ; temp = temp . next ; curr = temp ; } int res = temp . data ; return res ; } public static void main ( String args [ ] ) { int N = 100 ; System . out . println ( alivesol ( N ) ) ; } }