Input: head = [1,2,3,4,5,6,7,8,9,10,11,12,13], m = 2, n = 3Output: [1,2,6,7,11,12]Explanation: Keep the first (m = 2) nodes starting from the head of the linked List (1 -> 2) show in black nodes.Delete the next (n = 3) nodes (3 -> 4 -> 5) show in red nodes.Continue with the same procedure until reaching the tail of the Linked List.Head of the linked list after removing nodes is returned.
Input: head = [1,2,3,4,5,6,7,8,9,10,11], m = 1, n = 3Output: [1,5,9]Explanation: Head of linked list after removing nodes is returned.
from leetcode_py import ListNodeclass Solution: # Time: O(len(head)) # Space: O(1) def delete_nodes(self, head: ListNode[int] | None, m: int, n: int) -> ListNode[int] | None: pre = head while pre: for _ in range(m - 1): if pre.next: pre = pre.next cur = pre for _ in range(n): if cur.next: cur = cur.next pre.next = cur.next pre = pre.next return head