要删除LinkedList开头的节点,请使用RemoveFirst()
方法。
string [] employees = {"Peter","Robert","John","Jacob"}; LinkedList<string> list = new LinkedList<string>(employees);
现在,要删除第一个元素,请使用RemoveFirst()
方法。
list.RemoveFirst();
让我们看完整的例子。
using System; using System.Collections.Generic; class Demo { static void Main() { string [] employees = {"Peter","Robert","John","Jacob"}; LinkedList<string> list = new LinkedList<string>(employees); foreach (var emp in list) { Console.WriteLine(emp); } //删除第一个节点 list.RemoveFirst(); Console.WriteLine("LinkedList after删除第一个节点..."); foreach (var emp in list) { Console.WriteLine(emp); } } }
输出结果
Peter Robert John Jacob LinkedList after删除第一个节点... Robert John Jacob