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

相关推荐
wabs66637 分钟前
关于图论【最短路径之Bellman_ford 算法(单源有限最短路)|卡码网96.城市间货物运输III的思考】
数据结构·算法·图论·卡码网·bellman_ford·单源有限最短路
imaol11 小时前
链表 -- 环链表
java·前端·链表
imaol12 小时前
链表 -- 双向链表
java·前端·链表
lsylalalala3 小时前
常见的排序算法1
数据结构·算法·排序算法
想吃火锅10053 小时前
【leetcode】54. 螺旋矩阵
算法·leetcode·矩阵
想吃火锅10054 小时前
【leetcode】300. 最长递增子序列
算法·leetcode·职场和发展
imaol14 小时前
数据结构---队列
java·数据结构·算法
数模竞赛Paid answer4 小时前
2026年电工杯数学建模A题绿电直连型电氢氨园区优化运行求解全过程论文及程序
算法·数学建模·电工杯
疯狂打码的少年4 小时前
【数据结构】串的模式匹配:KMP算法(重点)
数据结构·笔记·算法
一米阳光86614 小时前
软考(中级)软件设计师核心笔记(8)数据结构——线性结构、数组、矩阵
数据结构·笔记·职场发展·软考·软件设计师·中级职称