백준 링크드 리스트 : 1158 - 요세푸스 문제

코린이서현이·2024년 1월 29일
0

🛎️ 1158번

정답 코드

class Node:
    def __init__(self,data,next=None):
        self.data = data
        self.next = next
class LinkedListCycle:
    def __init__(self,num):
        self.head = None
        current = None
        for i in range(1,num+1):
            if self.head is None:
                self.head = Node(i)
                current = self.head
            else:
                current.next = Node(i)
                current = current.next
        current.next = self.head
    def __str__(self):
        re = ""
        node = self.head
        if node is not None:
            re += str(node.data) +" "
            node = node.next

        while node is not self.head:
            re += str(node.data) +" "
            node = node.next
        return re

    def josephus_permutation(self,target):
        re = "<"
        current = self.head
        pre = current
        while pre.next is not self.head:
            pre = pre.next
        i = 1
        while current is not None:
            if i == target:
                i = 1
                if current.next is current:
                    re += str(current.data) +">"
                    current = None
                    return re
                pre.next = current.next
                re += str(current.data) + ", "
            else:
                i += 1
                pre = current
            current = current.next
        return re

x_list = [int(i) for i in input().split(" ")]

a_list = LinkedListCycle(x_list[0])
str = a_list.josephus_permutation(x_list[-1])

print(str)
profile
24년도까지 프로젝트 두개를 마치고 25년에는 개발 팀장을 할 수 있는 실력이 되자!

0개의 댓글