-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1474-DeleteNNodesAfterMNodesOfALinkedList.cs
40 lines (37 loc) · 1.16 KB
/
1474-DeleteNNodesAfterMNodesOfALinkedList.cs
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
31
32
33
34
35
36
37
38
39
40
//-----------------------------------------------------------------------------
// Runtime: 108ms
// Memory Usage: 29.6 MB
// Link: https://leetcode.com/submissions/detail/358299314/
//-----------------------------------------------------------------------------
namespace LeetCode
{
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int val=0, ListNode next=null) {
* this.val = val;
* this.next = next;
* }
* }
*/
public class _1474_DeleteNNodesAfterMNodesOfALinkedList
{
public ListNode DeleteNodes(ListNode head, int m, int n)
{
var dummy = new ListNode(-1);
dummy.next = head;
var curr = dummy;
while (curr != null)
{
int keep = m, remove = n;
while (keep-- > 0 && curr != null)
curr = curr.next;
while (remove-- > 0 && curr != null && curr.next != null)
curr.next = curr.next.next;
}
return dummy.next;
}
}
}