LeetCode 1823. Find the Winner of the Circular Game 解答

视频讲解

代码

class Solution
        # construct a double linked list node class
        class DoubleLinkedListNode:
            def __init__(self, val):
                self.val = val
                self.prev = None
                self.next = None
        
        # step-1: construct the circular double linked list
        head = DoubleLinkedListNode(1)
        curr = head
        for i in range(2,n+1):
            new_curr = DoubleLinkedListNode(i)
            new_curr.prev = curr
            curr.next = new_curr
            curr = new_curr
        curr.next = head
        head.prev = curr
            
        
        # step-2: do the loop ( check if the size is 1, if not, find the k-th element and delete)
        curr = head
        while curr.next != curr:
            for _ in range(k-1):
                curr = curr.next
            curr.prev.next = curr.next
            curr.next.prev = curr.prev
            curr = curr.next
        
        
        # step-3: return the result
        return curr.val

PPT讲解

本题涉及的知识点:

我的 Leetcode 讲解频道

代码链接

面试高频考点Youtube链接

面试低频考点Youtube链接

站内搜索 | Search


    视频 | Video Links

    Table of Contents