Skip to content

Commit 546988c

Browse files
committed
Sync LeetCode submission Runtime - 3 ms (47.52%), Memory - 19.4 MB (65.84%)
1 parent 73ef9e2 commit 546988c

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<p>You are given the <code>head</code> of a linked list and two integers <code>m</code> and <code>n</code>.</p>
2+
3+
<p>Traverse the linked list and remove some nodes in the following way:</p>
4+
5+
<ul>
6+
<li>Start with the head as the current node.</li>
7+
<li>Keep the first <code>m</code> nodes starting with the current node.</li>
8+
<li>Remove the next <code>n</code> nodes</li>
9+
<li>Keep repeating steps 2 and 3 until you reach the end of the list.</li>
10+
</ul>
11+
12+
<p>Return <em>the head of the modified list after removing the mentioned nodes</em>.</p>
13+
14+
<p>&nbsp;</p>
15+
<p><strong class="example">Example 1:</strong></p>
16+
<img alt="" src="https://assets.leetcode.com/uploads/2020/06/06/sample_1_1848.png" style="width: 600px; height: 95px;" />
17+
<pre>
18+
<strong>Input:</strong> head = [1,2,3,4,5,6,7,8,9,10,11,12,13], m = 2, n = 3
19+
<strong>Output:</strong> [1,2,6,7,11,12]
20+
<strong>Explanation:</strong> Keep the first (m = 2) nodes starting from the head of the linked List (1 -&gt;2) show in black nodes.
21+
Delete the next (n = 3) nodes (3 -&gt; 4 -&gt; 5) show in read nodes.
22+
Continue with the same procedure until reaching the tail of the Linked List.
23+
Head of the linked list after removing nodes is returned.
24+
</pre>
25+
26+
<p><strong class="example">Example 2:</strong></p>
27+
<img alt="" src="https://assets.leetcode.com/uploads/2020/06/06/sample_2_1848.png" style="width: 600px; height: 123px;" />
28+
<pre>
29+
<strong>Input:</strong> head = [1,2,3,4,5,6,7,8,9,10,11], m = 1, n = 3
30+
<strong>Output:</strong> [1,5,9]
31+
<strong>Explanation:</strong> Head of linked list after removing nodes is returned.
32+
</pre>
33+
34+
<p>&nbsp;</p>
35+
<p><strong>Constraints:</strong></p>
36+
37+
<ul>
38+
<li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code>.</li>
39+
<li><code>1 &lt;= Node.val &lt;= 10<sup>6</sup></code></li>
40+
<li><code>1 &lt;= m, n &lt;= 1000</code></li>
41+
</ul>
42+
43+
<p>&nbsp;</p>
44+
<p><strong>Follow up:</strong> Could you solve this problem by modifying the list in-place?</p>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Approach 1: Traverse Linked List and Delete In Place
2+
3+
# Time: O(n)
4+
# Space: O(1)
5+
6+
# Definition for singly-linked list.
7+
# class ListNode:
8+
# def __init__(self, val=0, next=None):
9+
# self.val = val
10+
# self.next = next
11+
class Solution:
12+
def deleteNodes(self, head: Optional[ListNode], m: int, n: int) -> Optional[ListNode]:
13+
current = head
14+
last = head
15+
16+
while current:
17+
m_count = m
18+
n_count = n
19+
20+
while current and m_count != 0:
21+
last = current
22+
current = current.next
23+
m_count -= 1
24+
25+
while current and n_count != 0:
26+
current = current.next
27+
n_count -= 1
28+
29+
last.next = current
30+
31+
return head
32+

0 commit comments

Comments
 (0)