Day5:移除链表元素

题目 :给你一个链表的头节点 head 和一个整数 val,请你删除链表中所有满足 Node.val == val 的节点,并返回新的头节点。

java 复制代码
public class Test {
    public ListNode removeElements(ListNode head,int val){
        if (head==null){
            return head;
        }
        ListNode prev=head;
        ListNode cur=head.next;
        while (cur != null){
            if (cur.val==val){
                prev.next=cur.next;
                cur=cur.next;
            }
            prev=cur;
            cur=cur.next;
        }
        if (head.val==val){
            head=head.next;
        }
        return  head;
    }
}

输入:head=1,2,6,3,2,6, val=6

输出:1,2,3,2

相关推荐
All for pursuit.6 小时前
【并查集-2】399.除法求值
数据结构·c++·算法·leetcode
迪丽热爱6 小时前
一、C语言概述与程序结构
c语言·开发语言·算法
码爸6 小时前
动态规划介绍
算法·动态规划
怪奇云呼军7 小时前
教育试听预约后没到课,闪电智能 Voice Agent 如何做分层回访?
人工智能·python·算法·云计算·音视频
Nil2087 小时前
leetcode 416分割等和子集
算法·leetcode·职场和发展
云淡风轻~窗明几净7 小时前
角谷猜想的进展之三 移动了群山/沸騰的群山 2026-10-10
人工智能·算法·dubbo·图论
ForeverYang20157 小时前
一种无需训练缺陷数据生成算法--O2MAG
人工智能·深度学习·算法
xuxigifxfh8 小时前
牛客:HJ170 01序列
java·开发语言·算法·华为机考
朝朝辞暮i8 小时前
C++ 第 33 课:事件循环——spin() 到底在干什么
开发语言·c++·算法
殷色玫瑰8 小时前
AVL树:从平衡因子到四种旋转的完整实现
c语言·数据结构·c++·算法·visualstudio