Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Josephus Circle Using Circular Linked List

Last Updated on July 27, 2023 by Mayank Dham

Josephus problem circular linked list is another name for Josephus problem linked list. This problem has become a common topic in recent interviews. Let’s explore the linked list approach to solving the Josephus problem.

As we have seen Different types of linked lists, We are aware of Circular linked lists. In this article, we are seeing an approach to Josephus problem circular linked list

How to Solve Josephus Problem Using Circular Linked List

In Josephus problem circular linked list N people are standing in a circle waiting to get executed. The counting 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 the number M, which indicates that M-1 persons are skipped and the Mth person is killed in the circle, your task is to choose the place in the initial circle so that you are the last one remaining (survive).

Understanding of Josephus Problem Circular Linked List with Example

Let us understand the problem with the help of an example:

Let’s take an input, where N = 5 and M = 3.

At this point, we have understood the problem statement ofJosephus problem circular linked list. Now we will try to formulate an approach to this problem.

Before moving to the approach ofJosephus problem circular linked list, try to think about how you can approach this problem.

  • If stuck, no problem, we will thoroughly see how we can approach this problem in the next section.

Let’s move to the approach section.

Approach of Josephus Problem Using Circular Linked List

  • We will create a circular linked list of size N, and each node will contain its position number in the list as its data.
  • Then, we will traverse the list and, every time delete the Mth node till we are left with only one node.
  • The left node at last is our solution to the problem.

Algorithm of Josephus Problem using Circular Linked List

  1. Create a new node and store its address in the head and also initialize a prev variable with the head.
  2. Run a loop and create a linked list of size N, attaching the nodes created in each iteration to the end of prev and then update the prev.
  3. After the loop is executed, connect the last node with the head of the list, i.e., prev->next = head.
  4. Initialize two pointers ptr1 and ptr2 with head.
  5. Run a while loop till the next node of ptr1 is ptr1 itself and inside the loop,

    • Initialize variable count with 1.
    • Run a while loop till the count is not equal to M.
    • Inside the loop, copy ptr2 in ptr1, move ptr1 forward by one node and increase count by one.
    • After the inner loop ends, remove the Mth node, i.e., ptr2->next = ptr1->next.
    • Update ptr1 with ptr2->next.
  6. After the loop ends, print the data of the last node left, i.e., data of ptr1.

Dry Run of Josephus Problem Circular Linked List

Code Implementation of Josephus Problem using Circular Linked List

Output of Josephus problem circular linked list

Last person left is 4

Time Complexity of Josephus problem circular linked list: O(M*N), where M and N are the inputs.

Conclusion of Josephus problem using circular linked list
So, in the aforementioned article, we attempted to explain how to most effectively solve the Josephus problem using a circular linked list. You can click on this link to access more questions on Linked List, which is maintained by PrepBytes’ knowledgeable mentors.

FAQs

Q1. What is a circular linked list?
Ans. A circular linked list is a linked list which is connected by the ends to form a circle-like structure.

Q2. What is the time complexity of a circular Linked list?
Ans. Operations such as Insertion which do not require traversal has a time complexity of O(1) and if the traversal is required then the time complexity is O(n).

Q3. How many null pointers exist in the circular linked list?
Ans. A circular Linked list is a doubly linked list which not only points to the next node but also points to the previous node as well. That’s why a circular linked list contains two null pointers.

Q4. How to implement the circular singly linked list?
Ans. To implement a circular singly linked list, we take an external pointer which points to the last node and the last next will be the first node.

Related Articles
Doubly circular linked list in data structure Application of doubly linked list
Applications of linked list Singly linked list vs doubly linked list
Advantages and disadvantages of linked list Doubly linked list all operations in C
Binary search in linked list Bubble sort linked list
Deletion in doubly linked list Delete the middle node of a linked list
Polynomial addition using linked list Find max value and min value in linked list
Insert a node at a specific position in a linked list Swap nodes in linked list
Add two numbers represented by linked lists Find starting point of loop in linked list
Merge sort linked list Delete a node from linked list at a given position
Remove duplicates from unsorted linked list Reverse a linked list in Python

Leave a Reply

Your email address will not be published. Required fields are marked *