| | | 1 | | // -------------------------------------------------------------------------------- |
| | | 2 | | // Copyright (C) 2026 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | | 3 | | // All Rights Reserved. |
| | | 4 | | // -------------------------------------------------------------------------------- |
| | | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | | 9 | | // known as Yevhenii Yeriemeieiv). |
| | | 10 | | // -------------------------------------------------------------------------------- |
| | | 11 | | |
| | | 12 | | using LeetCode.Core.Models; |
| | | 13 | | |
| | | 14 | | namespace LeetCode.Algorithms.DeleteNodeFromLinkedListPresentInArray; |
| | | 15 | | |
| | | 16 | | /// <inheritdoc /> |
| | | 17 | | public sealed class DeleteNodeFromLinkedListPresentInArrayHashSet : IDeleteNodeFromLinkedListPresentInArray |
| | | 18 | | { |
| | | 19 | | /// <summary> |
| | | 20 | | /// Time complexity - O(n + m) |
| | | 21 | | /// Space complexity - O(n) |
| | | 22 | | /// </summary> |
| | | 23 | | /// <param name="nums"></param> |
| | | 24 | | /// <param name="head"></param> |
| | | 25 | | /// <returns></returns> |
| | | 26 | | public ListNode? ModifiedList(int[] nums, ListNode? head) |
| | 5 | 27 | | { |
| | 5 | 28 | | if (nums.Length == 0) |
| | 1 | 29 | | { |
| | 1 | 30 | | return head; |
| | | 31 | | } |
| | | 32 | | |
| | 4 | 33 | | var numsHashSet = new HashSet<int>(nums); |
| | | 34 | | |
| | 4 | 35 | | var dummyHead = new ListNode(0, head); |
| | | 36 | | |
| | 4 | 37 | | var node = dummyHead; |
| | | 38 | | |
| | 45 | 39 | | while (node?.next != null) |
| | 41 | 40 | | { |
| | 41 | 41 | | if (numsHashSet.Contains(node.next.val)) |
| | 21 | 42 | | { |
| | 21 | 43 | | node.next = node.next.next; |
| | 21 | 44 | | } |
| | | 45 | | else |
| | 20 | 46 | | { |
| | 20 | 47 | | node = node.next; |
| | 20 | 48 | | } |
| | 41 | 49 | | } |
| | | 50 | | |
| | 4 | 51 | | return dummyHead.next; |
| | 5 | 52 | | } |
| | | 53 | | } |