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 ; Del represent next person to be deleted or killed ; Last two person to survive ( in any order ) ; Driver code
class Node { constructor ( val ) { this . val = val ; this . next = null ; } } ; function getLastTwoPerson ( n ) { var total = n ; var head = new Node ( 1 ) ; var temp = head ; for ( var i = 2 ; i <= n ; i ++ ) { temp . next = new Node ( i ) ; temp = temp . next ; } temp . next = head ; temp = head ; var del ; while ( total != 2 ) { del = temp . next . next ; temp . next . next = temp . next . next . next ; temp = temp . next ; total -= 1 ; } document . write ( temp . val + " " + temp . next . val ) ; } var n = 2 ; getLastTwoPerson ( n ) ;