C# 中的 LinkedList RemoveLast() 方法

csharpprogrammingserver side programming更新于 2025/8/11 5:52:17

首先,声明一个 LinkedList 并添加节点。

int [] num = {92, 98, 110, 130, 145, 170, 230, 240, 250, 300, 330};
LinkedList<int> myList = new LinkedList<int>(num);

使用 RemoveLast() 方法从 LinkedList 中移除最后一个节点。

myList.RemoveLast();

示例

using System;
using System.Collections.Generic;
class Demo {
   static void Main() {
      int [] num = {92, 98, 110, 130, 145, 170, 230, 240, 250, 300, 330};
      LinkedList<int> myList = new LinkedList<int>(num);
      foreach (var n in myList) {
         Console.WriteLine(n);
      }
      // 移除最后一个节点
      myList.RemoveLast();
      Console.WriteLine("LinkedList after removing the last node...");
      foreach (var n in myList) {
         Console.WriteLine(n);
      }
   }
}

输出

92
98
110
130
145
170
230
240
250
300
330
LinkedList after removing the last node...
92
98
110
130
145
170
230
240
250
300

相关文章