首页 > 其他分享 >Leetcode --- 203. 移除链表元素

Leetcode --- 203. 移除链表元素

时间:2024-05-07 21:34:24浏览次数:15  
标签:203 ListNode val head next 链表 移除 prev

题目描述

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

示例 1:

 示例

输入:head = [1,2,6,3,4,5,6], val = 6
输出:[1,2,3,4,5]
输入:head = [7,7,7,7], val = 7
输出:[]

参考实现

方式1、使用递归实现 

    /**
     * 递归实现删除链表元素
     *
     * @param head
     * @param val
     * @return
     */
    public static ListNode removeElements(ListNode head, int val) {
        if (head == null) {
            return null;
        } else {
            head.next = removeElements(head.next, val);
            if (head.val == val) {
                return head.next;
            } else {
                return head;
            }
        }
    }

方式2、使用循环实现 

    /**
     * 循环删除链表元素
     *
     * @param head
     * @param val
     * @return
     */
    public static ListNode removeElementsxh(ListNode head, int val) {
        //声明虚拟节点
        ListNode dummy = new ListNode(val - 1);
        dummy.next = head;
        //复制出来
        ListNode prev = dummy;
        while (prev.next != null) {
            //满足删除条件进行删除
            if (prev.next.val == val) {
                prev.next = prev.next.next;
            } else {
                //不满足进行遍历
                prev = prev.next;
            }
        }
        return dummy.next;
    }

 

标签:203,ListNode,val,head,next,链表,移除,prev
From: https://www.cnblogs.com/wdh01/p/17487521.html

相关文章

  • 自定义单链表(非循环)的基本接口函数
    文件描述及头文件包含/********************************************************************* 文件名称: 单链表(非循环)的基本接口程序* 文件作者:[email protected]* 创建日期:2024/05/07* 文件功能:对单链表的增删改查功能的定义* 注意事项:No......
  • 自定义单链表(非循环)反转的基本函数接口
    题干structListNode*ReverseList(structListNode*head){if(head==NULL||head->next==NULL){returnhead;}else{structListNode*Phead=head;structListNode*temp=head->next;Phead->next=NULL;......
  • 双向循环链表的实现
    /********************************************************************************************************** filename: Zqh_链表.c* author : [email protected]* date : 2024/05/05* function: 链表的增删改查* note : 模板* *Copyright(c)2023-202......
  • 双向链表实现
    /********************************************************************************************************** filename: Zqh_链表.c* author : [email protected]* date : 2024/05/05* function: 链表的增删改查* note : 模板* *Copyright(c)2023-202......
  • 单向循环链表的实现
    /********************************************************************************************************** filename: Zqh_链表.c* author : [email protected]* date : 2024/05/05* function: 链表的增删改查* note : 模板* *Copyright(c)2023-202......
  • 单链表逆序
    逆序原理:保留头节点下一个结点地址,将头节点断开,遍历除头节点以外的节点,将那些节点头插入头节点中。就能实习逆序。/********************************************************************* filename: demo2.c* author :lzj* date :2024/04/23* function:单向链......
  • 头插法新建链表
    新建链表指针结构体typedefintElemType;typedefstructLNode{ ElemTypedata; structLNode*next;//指向下一个结点}LNode,*LinkList;//结构体名LNode==*LinkList;LNode*==LinkList头插法新建链表先建立一个头结点:L=(LinkList)malloc(sizeof(LNode));//带......
  • 尾插法新建链表
    核心代码:tail=head;s->next=NULL;tail->next=s;tail=s;插入过程演示:![[Pastedimage20230623143820.png]]头插法尾插法新建链表完整代码#include<iostream>#include<malloc.h>usingnamespacestd;typedefintElemtype;typedefstructLNode{ El......
  • c#数组移除同一个值
    数组移除数据,需要循环覆盖的方法。可以快慢双指针。循环一遍。publicintRemoveElement(int[]nums,intval){intn=nums.Length;intlow=0;for(inti=0;i<n;i++){if(nums[i]!=val){nums[low]=nums[......
  • 考研打卡链表,栈,队列
    1:链表点击查看代码#include<bits/stdc++.h>usingnamespacestd;typedefstructnode{ intdata; structnode*next;}listnode,*list1;typedefstructnow{ intdata; structnow*next,*prve;}listnow,*list2;boollistn(list1&L,intx,inty){ if(x&l......