C# 通过索引从单链表中删除

C# 通过索引从单链表中删除,c#,generics,collections,linked-list,C#,Generics,Collections,Linked List,如何在不使用尾部的情况下通过索引插入到通用链表 public void RemoveByIndex(int index) { int count = 0; LinkedListNode<T> current = Head; while (count <= index) { if (count == index) { current.Next = null; }

如何在不使用尾部的情况下通过索引插入到通用链表

public void RemoveByIndex(int index)
{
    int count = 0;
    LinkedListNode<T> current = Head;
    while (count <= index)
    {
        if (count == index)
        {
            current.Next = null;
        }
        else
        {
            current = current.Next;
        }
        count++;
    }
    Head = current;
}
public void RemoveByIndex(int-index)
{
整数计数=0;
LinkedListNode当前=头;
而(count
var list1=new LinkedList();
var nodesToRemove=list1.Where(x=>x==index.ToList();
foreach(nodesToRemove中的var节点)
lis1.移除(节点);
第一次迭代(计数=0)=>
否则执行块=>
current=current.next
=>current=10,current.next=15

第二次迭代(计数=1)=>
如果执行
块=>
current.next=null
=>15被删除=>current=10

在第二次迭代结束时,count=2,退出while循环=>
Head=current
=>Head=10=>5被删除

可能的解决办法:

public void RemoveByIndex(int index)
{
   int count = 0;
   LinkedListNode<T> current = Head;
   LinkedListNode<T> previous = current;
   while (count <= index)
   {
       if (count == index)
       {
           previous.next = current.next               
           current = null;               
       }
       else
       {
           previous = current;
           current = current.Next;
       }           
       count++;
   }       
} 
public void RemoveByIndex(int-index)
{
整数计数=0;
LinkedListNode当前=头;
LinkedListNode previous=当前;

当你从列表中删除时,为什么标题会说“添加”?编辑,抱歉,今天的代码太多了。谢谢,但是,我正在用scatch制作单链表,使用泛型类型和Head。
public void RemoveByIndex(int index)
{
   int count = 0;
   LinkedListNode<T> current = Head;
   LinkedListNode<T> previous = current;
   while (count <= index)
   {
       if (count == index)
       {
           previous.next = current.next               
           current = null;               
       }
       else
       {
           previous = current;
           current = current.Next;
       }           
       count++;
   }       
}