-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSublistSearch.cs
More file actions
36 lines (31 loc) · 1.13 KB
/
SublistSearch.cs
File metadata and controls
36 lines (31 loc) · 1.13 KB
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
using AlgorithmsAndDataStructures.DataStructures.LinkedList;
namespace AlgorithmsAndDataStructures.Algorithms.Search;
public class SublistSearch
{
/*
Time Complexity : O(m*n)
where m is the number of nodes in second list and n in first.
*/
#pragma warning disable CA1822 // Mark members as static
public bool IsSubset(SinglyLinkedList<int> input, SinglyLinkedList<int> pattern)
#pragma warning restore CA1822 // Mark members as static
{
if (input is null || pattern is null) return default;
var patternPointer = pattern.GetHead();
var inputPointer = input.GetHead();
while (inputPointer != null && patternPointer != null)
if (inputPointer.Value != patternPointer.Value)
{
if (patternPointer != pattern.GetHead())
patternPointer = pattern.GetHead();
else
inputPointer = inputPointer.Next;
}
else
{
patternPointer = patternPointer.Next;
inputPointer = inputPointer.Next;
}
return patternPointer == null;
}
}