在C#开发中,链表是一种灵活的数据结构,相比数组不需要连续的内存空间,插入和删除节点的效率更高。当我们需要在链表中获取特定值的节点或者符合特定条件的节点时,就需要实现对应的查找逻辑。C#中既可以使用内置的LinkedList<T>类型,也可以自定义链表结构来实现查找功能。

使用内置LinkedList<T>查找节点
C#的System.Collections.Generic命名空间提供了LinkedList<T>泛型链表类,该类本身没有直接的查找方法,但我们可以通过遍历的方式实现节点查找。
查找指定值的节点
下面的示例演示了如何在LinkedList<T>中查找第一个值等于目标值的节点:
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
// 创建链表并添加元素
LinkedList<int> linkedList = new LinkedList<int>();
linkedList.AddLast(10);
linkedList.AddLast(20);
linkedList.AddLast(30);
linkedList.AddLast(20);
int targetValue = 20;
LinkedListNode<int> targetNode = null;
// 遍历链表查找节点
LinkedListNode<int> currentNode = linkedList.First;
while (currentNode != null)
{
if (currentNode.Value == targetValue)
{
targetNode = currentNode;
break; // 找到第一个匹配的节点就终止遍历
}
currentNode = currentNode.Next;
}
if (targetNode != null)
{
Console.WriteLine($"找到目标节点,值为:{targetNode.Value}");
// 可以操作找到的节点,比如获取下一个节点的值
if (targetNode.Next != null)
{
Console.WriteLine($"目标节点的下一个节点值为:{targetNode.Next.Value}");
}
}
else
{
Console.WriteLine("未找到目标节点");
}
}
}
查找符合自定义条件的节点
如果查找条件不是简单的等值匹配,而是需要满足自定义规则,我们可以修改遍历的判断逻辑:
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
LinkedList<string> nameList = new LinkedList<string>();
nameList.AddLast("张三");
nameList.AddLast("李四");
nameList.AddLast("王五");
nameList.AddLast("赵六");
// 查找长度大于2的字符串节点
LinkedListNode<string> resultNode = null;
LinkedListNode<string> current = nameList.First;
while (current != null)
{
if (current.Value.Length > 2)
{
resultNode = current;
break;
}
current = current.Next;
}
if (resultNode != null)
{
Console.WriteLine($"找到符合条件的节点,值为:{resultNode.Value}");
}
else
{
Console.WriteLine("未找到符合条件的节点");
}
}
}
自定义单向链表实现节点查找
如果我们需要更灵活的链表控制,也可以自定义单向链表结构,实现节点的查找功能:
using System;
// 自定义链表节点类
class MyListNode<T>
{
public T Value { get; set; }
public MyListNode<T> Next { get; set; }
public MyListNode(T value)
{
Value = value;
Next = null;
}
}
// 自定义单向链表类
class MyLinkedList<T>
{
public MyListNode<T> Head { get; private set; }
public void AddLast(T value)
{
MyListNode<T> newNode = new MyListNode<T>(value);
if (Head == null)
{
Head = newNode;
return;
}
MyListNode<T> current = Head;
while (current.Next != null)
{
current = current.Next;
}
current.Next = newNode;
}
// 查找指定值的节点
public MyListNode<T> Find(T target)
{
MyListNode<T> current = Head;
while (current != null)
{
// 使用Equals方法比较值,避免值类型比较问题
if (current.Value.Equals(target))
{
return current;
}
current = current.Next;
}
return null;
}
}
class Program
{
static void Main()
{
MyLinkedList<int> myList = new MyLinkedList<int>();
myList.AddLast(5);
myList.AddLast(15);
myList.AddLast(25);
int searchValue = 15;
MyListNode<int> node = myList.Find(searchValue);
if (node != null)
{
Console.WriteLine($"自定义链表中找到目标节点,值为:{node.Value}");
}
else
{
Console.WriteLine("自定义链表中未找到目标节点");
}
}
}
查找注意事项
- 遍历链表时一定要注意终止条件,避免出现无限循环,内置
LinkedList<T>的节点Next属性在最后一个节点会返回null,自定义链表也需要保证尾节点的Next为null。 - 如果链表可能为空,在遍历前需要先判断头节点或者第一个节点是否为
null,避免空引用异常。 - 对于值类型的比较,直接使用
==即可,对于引用类型,建议使用Equals方法进行比较,或者提前确定比较规则。 - 如果需要查找所有符合条件的节点,而不是第一个,就不要提前
break遍历,而是把符合条件的节点都收集到集合中再返回。
不同查找方式性能对比
链表查找的时间复杂度为O(n),n为链表长度,因为最坏情况下需要遍历所有节点。下面是内置链表和自定义链表查找的特点对比:
| 链表类型 | 查找实现难度 | 额外功能支持 | 适用场景 |
|---|---|---|---|
| 内置LinkedList<T> | 低,只需遍历即可 | 支持节点前后插入、删除,有现成的节点操作API | 常规链表操作,不需要自定义特殊逻辑的场景 |
| 自定义链表 | 较高,需要实现节点和链表结构 | 可根据需求扩展功能,比如添加索引、缓存等 | 需要特殊链表逻辑,或者对链表有定制需求的场景 |
C#链表查找节点LinkedList修改时间:2026-07-18 12:42:27