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

相关推荐
Tyler_TXZ1 小时前
C++C语言之——树
c语言·c++·算法·
旖旎夜光1 小时前
LeetCode 30:串联所有单词的子串(滑动窗口) —— 题解
数据结构·c++·算法·leetcode·滑动窗口
白狐_7981 小时前
408 数据结构|完全二叉树两道典型题:第 6 层叶结点数与叶结点总数
数据结构·算法
lucas_AI1 小时前
喂张白纸也能吐出证件号?文档 MLLM 的"关系级泄露"被测出来了
人工智能·算法·掘金技术征文
手写码匠2 小时前
华为云Flexus+DeepSeek征文|Dify 多 Agent 灰度发布实战:让每一次变更都“小步快跑、随时可回滚“
人工智能·深度学习·算法·aigc
阿部多瑞 ABU2 小时前
从0到1:用 Spring Boot 3.4 + Vue3 做一个能“智能排道次“的运动会编排系统(附核心算法)
java·spring boot·后端·算法·spring
Nil2082 小时前
leetcode 48旋转图像
算法·leetcode·职场和发展
嘟嘟07173 小时前
顺时针螺旋填充 n×n 矩阵:手撕 generateMatrix 的四个 for 循环
javascript·算法·面试
Nil2083 小时前
leetcode 206反转链表
算法·leetcode·链表
Kstheme3 小时前
大模型内部是怎么运作的?从「一个神经元」拆到「残差流」
算法