Lucky alive person in a circle | Code Solution to sword puzzle | C # 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
using System ; class GFG { public class Node { public int data ; public 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 tem = newNode ( i ) ; tem . next = last . next ; last . next = tem ; last = tem ; } Node curr = last . next ; Node tem1 = new Node ( ) ; while ( curr . next != curr ) { tem1 = curr ; curr = curr . next ; tem1 . next = curr . next ; tem1 = tem1 . next ; curr = tem1 ; } int res = tem1 . data ; return res ; } public static void Main ( String [ ] args ) { int N = 100 ; Console . WriteLine ( alivesol ( N ) ) ; } }