-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path1474.py
30 lines (28 loc) · 780 Bytes
/
1474.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
# TAGS: Premium
class Solution:
# 72 ms, 76.4%
def deleteNodes(self, head: ListNode, m: int, n: int) -> ListNode:
cnt = 0
flag = True
node = prev = head
while node:
nxt = node.next
cnt += 1
if flag:
prev.next = node
prev = node
if cnt == m:
cnt = 0
flag = not flag
else:
prev.next = None
if cnt == n:
cnt = 0
flag = not flag
node = nxt
return head