|
| 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> </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 ->2) show in black nodes. |
| 21 | +Delete the next (n = 3) nodes (3 -> 4 -> 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> </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 <= Node.val <= 10<sup>6</sup></code></li> |
| 40 | + <li><code>1 <= m, n <= 1000</code></li> |
| 41 | +</ul> |
| 42 | + |
| 43 | +<p> </p> |
| 44 | +<p><strong>Follow up:</strong> Could you solve this problem by modifying the list in-place?</p> |
0 commit comments