Skip to content
Related Articles

Related Articles

Josephus Circle using circular linked list
  • Difficulty Level : Medium
  • Last Updated : 04 Dec, 2020

There are n people standing in a circle waiting to be executed. The counting out begins at some point in the circle and proceeds around the circle in a fixed direction. In each step, a certain number of people are skipped and the next person is executed. The elimination proceeds around the circle (which is becoming smaller and smaller as the executed people are removed), until only the last person remains, who is given freedom. Given the total number of persons n and a number m which indicates that m-1 persons are skipped and m-th person is killed in circle. The task is to choose the place in the initial circle so that you are the last one remaining and so survive.
Examples : 
 

Input : Length of circle : n = 4
        Count to choose next : m = 2
Output : 1

Input : n = 5
        m = 3
Output : 4

 

We have discussed different solutions of this problem (here and here). In this post a simple circular linked list based solution is discussed. 
1) Create a circular linked list of size n. 
2) Traverse through linked list and one by one delete every m-th node until there is one node left. 
3) Return value of the only left node.
 

C++




// CPP program to find last man standing
#include<bits/stdc++.h>
using namespace std;
 
/* structure for a node in circular
   linked list */
struct Node
{
    int data;
    struct Node *next;
};
 
// To create a new node of circular
// linked list
Node *newNode(int data)
{
   Node *temp = new Node;
   temp->next = temp;
   temp->data = data;
}
 
/* Function to find the only person left
   after one in every m-th node is killed
   in a circle of n nodes */
void getJosephusPosition(int m, int n)
{
    // Create a circular linked list of
    // size N.
    Node *head = newNode(1);
    Node *prev = head;
    for (int i = 2; i <= n; i++)
    {
        prev->next = newNode(i);
        prev = prev->next;
    }
    prev->next = head; // Connect last
                       // node to first
 
    /* while only one node is left in the
    linked list*/
    Node *ptr1 = head, *ptr2 = head;
    while (ptr1->next != ptr1)
    {
        // Find m-th node
        int count = 1;
        while (count != m)
        {
            ptr2 = ptr1;
            ptr1 = ptr1->next;
            count++;
        }
 
        /* Remove the m-th node */
        ptr2->next = ptr1->next;
        free(ptr1);
        ptr1 = ptr2->next;
    }
 
    printf ("Last person left standing "
            "(Josephus Position) is %d\n ",
            ptr1->data);
}
 
/* Driver program to test above functions */
int main()
{
    int n = 14, m = 2;
    getJosephusPosition(m, n);
    return 0;
}


Java




// Java Code to find the last man Standing
public class GFG {
     
    // Node class to store data
    static class Node
    {
        public int data ;
        public Node next;
        public Node( int data )
        {
            this.data = data;
        }
    }
     
    /* Function to find the only person left
    after one in every m-th node is killed
    in a circle of n nodes */
    static void getJosephusPosition(int m, int n)
    {
        // Create a circular linked list of
        // size N.
        Node head = new Node(1);
        Node prev = head;
        for(int i = 2; i <= n; i++)
        {
            prev.next = new Node(i);
            prev = prev.next;
        }
         
        // Connect last node to first
        prev.next = head;
         
        /* while only one node is left in the
        linked list*/
        Node ptr1 = head, ptr2 = head;
         
        while(ptr1.next != ptr1)
        {
             
            // Find m-th node
            int count = 1;
            while(count != m)
            {
                ptr2 = ptr1;
                ptr1 = ptr1.next;
                count++;
            }
             
            /* Remove the m-th node */
            ptr2.next = ptr1.next;
            ptr1 = ptr2.next;
        }
        System.out.println ("Last person left standing " +
                 "(Josephus Position) is " + ptr1.data);
    }
     
    /* Driver program to test above functions */
    public static void main(String args[])
    {
        int n = 14, m = 2;
        getJosephusPosition(m, n);
    }
}


Python3




# Python3 program to find last man standing
 
# /* structure for a node in circular
#    linked list */
class Node:
    def __init__(self, x):
        self.data = x
        self.next = None
 
# /* Function to find the only person left
#    after one in every m-th node is killed
#    in a circle of n nodes */
def getJosephusPosition(m, n):
   
    # Create a circular linked list of
    # size N.
    head = Node(1)
    prev = head
    for i in range(2, n + 1):
        prev.next = Node(i)
        prev = prev.next
    prev.next = head # Connect last
                       #node to first
 
    #/* while only one node is left in the
    #linked list*/
    ptr1 = head
    ptr2 = head
    while (ptr1.next != ptr1):
        # Find m-th node
        count = 1
        while (count != m):
            ptr2 = ptr1
            ptr1 = ptr1.next
            count += 1
 
        # /* Remove the m-th node */
        ptr2.next = ptr1.next
        # free(ptr1)
        ptr1 = ptr2.next
 
    print("Last person left standing (Josephus Position) is ", ptr1.data)
 
# /* Driver program to test above functions */
if __name__ == '__main__':
    n = 14
    m = 2
    getJosephusPosition(m, n)
 
# This code is contributed by mohit kumar 29


C#




// C# Code to find the last man Standing
using System;
public class GFG {
     
    // Node class to store data
    class Node
    {
        public int data ;
        public Node next;
        public Node( int data )
        {
            this.data = data;
        }
    }
     
    /* Function to find the only person left
    after one in every m-th node is killed
    in a circle of n nodes */
    static void getJosephusPosition(int m, int n)
    {
        // Create a circular linked list of
        // size N.
        Node head = new Node(1);
        Node prev = head;
        for(int i = 2; i <= n; i++)
        {
            prev.next = new Node(i);
            prev = prev.next;
        }
         
        // Connect last node to first
        prev.next = head;
         
        /* while only one node is left in the
        linked list*/
        Node ptr1 = head, ptr2 = head;
         
        while(ptr1.next != ptr1)
        {
             
            // Find m-th node
            int count = 1;
            while(count != m)
            {
                ptr2 = ptr1;
                ptr1 = ptr1.next;
                count++;
            }
             
            /* Remove the m-th node */
            ptr2.next = ptr1.next;
            ptr1 = ptr2.next;
        }
        Console.WriteLine ("Last person left standing " +
                "(Josephus Position) is " + ptr1.data);
    }
     
    /* Driver program to test above functions */
     static public void Main(String []args)
    {
        int n = 14, m = 2;
        getJosephusPosition(m, n);
    }
}
//contributed by Arnab Kundu


Output : 
 

Last person left standing (Josephus Position) is 13

Time complexity: O(m * n)
This article is contributed by Raghav Sharma. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :