# Josephus Circle using circular linked list

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 = 2Output : 1Input : n = 5        m = 3Output : 4`

code:

`def increment_by(self,end,start = 0):    temp = self.head    if temp == None:        for i in range(start,end):            self.insert(i)    else:        for i in range(start,end):            if temp.next == self.head:                self.insert(temp.data+1)                temp = temp.next            else:                temp = temp.nextdef JosephusCircle(self,n,k):    self.increment_by(n+1,start=1)    temp = self.head    if temp == None:        return    if self.head.next == self.head:        return temp.data    while(temp):        if temp.next.data == temp.data:            return temp.data        skip = temp.next        for i in range(k):            temp = temp.next        self.delete(skip.data)`

test:

`class Node():    def __init__(self,data):        self.data = data        self.next = Noneclass CircularLinkedList():    def __init__(self):        self.head = None    def insert(self,new_data):        temp = self.head        new_node = Node(new_data)        if temp == None:            self.head = new_node            new_node.next = self.head            return        while(temp):            if temp.next == self.head:                temp.next = new_node                new_node.next = self.head                return            if temp.data < new_data:                prev = temp                temp = temp.next            if temp.data>new_data:                prev.next = new_node                new_node.next = temp                return    def delete(self,element):        temp = self.head        if temp.data == element:            while (temp.next != self.head):                temp = temp.next            temp.next = self.head.next            self.head = temp.next            return        while(temp):            if temp.data == element:                if temp.next == self.head:                    prev.next = self.head                    temp = Node                    return                else:                    prev.next = temp.next                    return            else:                prev  = temp                temp = temp.next                if temp == self.head:                    print('element not found')                    break    def increment_by(self,end,start = 0):        temp = self.head        if temp == None:            for i in range(start,end):                self.insert(i)        else:            for i in range(start,end):                if temp.next == self.head:                    self.insert(temp.data+1)                    temp = temp.next                else:                    temp = temp.next    def JosephusCircle(self,n,k):        self.increment_by(n+1,start=1)        temp = self.head        if temp == None:            return        if self.head.next == self.head:            return temp.data        while(temp):            if temp.next.data == temp.data:                return temp.data            skip = temp.next            for i in range(k):                temp = temp.next            self.delete(skip.data)list2 = CircularLinkedList()print(list2.JosephusCircle(4,2))`

output:

`1`

Looking for my next opportunity to make change in a BIG way

## More from Sairam Penjarla

Looking for my next opportunity to make change in a BIG way