当前位置: 首页 > news >正文

【链表】- 移除链表元素

1. 对应力扣题目连接

  • 移除链表元素

2. 实现案例代码

public class RemoveLinkedListElements {public static void main(String[] args) {// 示例 1ListNode head1 = new ListNode(1, new ListNode(2, new ListNode(6, new ListNode(3, new ListNode(4, new ListNode(5, new ListNode(6)))))));ListNode result1 = removeElements(head1, 6);printList(result1); // 输出 [1, 2, 3, 4, 5]}public static ListNode removeElements(ListNode head, int val) {// 创建一个虚拟头节点,以防止删除头节点时需要特殊处理ListNode dummy = new ListNode(0);dummy.next = head;ListNode current = dummy;// 遍历链表while (current.next != null) {if (current.next.val == val) {// 移除节点current.next = current.next.next;} else {current = current.next;}}// 返回新的头节点return dummy.next;}private static void printList(ListNode head) {ListNode current = head;while (current != null) {System.out.print(current.val);current = current.next;if (current != null) {System.out.print(" -> ");}}System.out.println();}
}/*** 节点类*/
class ListNode {int val;ListNode next;ListNode() {}ListNode(int val) {this.val = val;}ListNode(int val, ListNode next) {this.val = val;this.next = next;}
}
http://www.lryc.cn/news/388873.html

相关文章:

  • 云原生之使用Docker部署RabbitMQ消息中间件
  • opengl箱子的显示
  • Oracle 视图、存储过程、函数、序列、索引、同义词、触发器
  • 网站被浏览器提示“不安全”的解决办法
  • typescript定义函数的传参、返回值
  • GlimmerHMM安装与使用-生信工具24
  • Elasticsearch架构基本原理
  • STM32自己从零开始实操08:电机电路原理图
  • 无线物联网练习题
  • Java的日期类常用方法
  • 数据库设计规范详解
  • Android12 MultiMedia框架之MediaExtractorService
  • Chapter 8 Feedback
  • Administrators就最高了???system是什么??本地用户提权内网学习第三天 你知道uac是什么??
  • 回溯 | Java | LeetCode 39, 40, 131 做题总结(未完)
  • Linux系统上部署Whisper。
  • 申请一张含100个域名的证书-免费SSL证书
  • 爬数据是什么意思?
  • Pytorch实战(二)
  • wordpress 付费主题modown分享,可实现资源付费
  • 【INTEL(ALTERA)】NIOS II调试器中的重新启动按钮不起作用
  • Hive On Spark语法
  • 利用 fail2ban 保护 SSH 服务器
  • 在TkinterGUI界面显示WIFI网络摄像头(ESP32s3)视频画面
  • Yolov8训练时遇到报错SyntaxError: ‘image_weights‘ is not a valid YOLO argument.等问题解决方案
  • javaweb(四)——过滤器与监听器
  • 冗余电源的应用,哪些工作站支持冗余电源
  • [信号与系统]IIR滤波器与FIR滤波器相位延迟定量的推导。
  • Python海量数据处理脚本大集合:pyWhat
  • postgresql搭建