LinkedList<T>.Contains() kiểm tra xem giá trị chỉ định có tồn tại trong danh sách liên kết hay không. Trả về true/false. Độ phức tạp O(n).
Ví dụ minh họa
using System;
using System.Collections.Generic;
class Demo
{
static void Main()
{
int[] num = { 1, 3, 7, 15 };
LinkedList<int> list = new LinkedList<int>(num);
foreach (var n in list)
Console.WriteLine(n);
var newNode = list.AddLast(20);
list.AddAfter(newNode, 30);
Console.WriteLine("LinkedList sau khi thêm node...");
foreach (var n in list)
Console.WriteLine(n);
Console.WriteLine("Số 3 (node) có trong danh sách? " + list.Contains(3));
}
}
Kết quả đầu ra
1 3 7 15 LinkedList sau khi thêm node... 1 3 7 15 20 30 Số 3 (node) có trong danh sách? True
Lưu ý: Contains() tìm kiếm tuyến tính (O(n)). Với tra cứu thường xuyên, cân nhắc HashSet<T> hoặc Dictionary<TKey,TValue>.