cs: add «3217. Delete Nodes From Linked List Present in Array»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
add853b5e4
commit
548b824d4a
2 changed files with 30 additions and 0 deletions
9
cs/ListNode.cs
Normal file
9
cs/ListNode.cs
Normal 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;
|
||||
}
|
||||
}
|
21
cs/delete-nodes-from-linked-list-present-in-array.cs
Normal file
21
cs/delete-nodes-from-linked-list-present-in-array.cs
Normal 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;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue