cs: add «3217. Delete Nodes From Linked List Present in Array»

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-09-06 19:08:55 +02:00
parent add853b5e4
commit 548b824d4a
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8
2 changed files with 30 additions and 0 deletions

9
cs/ListNode.cs Normal file
View file

@ -0,0 +1,9 @@
public class ListNode {
public int val;
public ListNode? next;
public ListNode(int val = 0, ListNode? next = null) {
this.val = val;
this.next = next;
}
}

View file

@ -0,0 +1,21 @@
using System.Collections.Generic;
public class Solution {
public ListNode ModifiedList(int[] nums, ListNode head) {
var uniqueNums = new HashSet<int>(nums);
var node = head;
while (node.next != null) {
if (uniqueNums.Contains(node.next.val)) {
node.next = node.next.next;
} else {
node = node.next;
}
}
if (uniqueNums.Contains(head.val)) {
return head.next;
}
return head;
}
}