删除链表中所有值为val的节点

public class Node {
    int  val;
    Node  next=null;
    Node(int  val){
        this.val=val;
        this.next=null;
    }
    public String toString(){
        return  String.format("Node(%d)",val);
    }
}

public class RemoveElements {//删除值为val的节点
    public static Node removeElements(Node  head,int val){
        Node result=null;
        Node last=null;
        Node cur=head;
        while(cur!=null){//遍历所有的节点
            if(cur.val==val){
                cur=cur.next;
                continue;
            }
            //走到这里说明cur.val!=val
            Node  next=cur.next;
            cur.next=null;
            if(result==null){
                result=cur;
            }else{
                last.next=cur;
            }
            last=cur;
            cur=next;
        }
        return result;
    }
   public  static  void Print(Node  head){
        Node  cur=head;
        while(cur!=null){
            System.out.print(cur.val+"-->");
            cur=cur.next;
        }
       System.out.println("null");
   }

    public static void main(String[] args) {
        Node  l1=new  Node(1);
        Node  l2=new  Node(5);
        Node  l3=new  Node(2);
        Node  l4=new  Node(5);
        Node  l5=new  Node(3);
        Node  l6=new  Node(2);
        Node  l7=new  Node(5);
        l1.next=l2;
        l2.next=l3;
        l3.next=l4;
        l4.next=l5;
        l5.next=l6;
        l6.next=l7;
        l7.next=null;
        System.out.print("删除前:");
        Print(l1);
        System.out.print("删除后:");
        removeElements(l1,5);
        Print(l1);
    }
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是用 C 语言写的删除链表等于给定 val 的所有节点的代码,并附有注释: ```c #include <stdio.h> #include <stdlib.h> // 定义链表节点结构体 struct ListNode { int val; struct ListNode *next; }; // 定义删除链表等于给定 val 的所有节点的函数 struct ListNode* removeElements(struct ListNode* head, int val) { // 如果链表为空,直接返回 if (head == NULL) { return NULL; } // 如果链表节点等于 val,需要特殊处理 while (head != NULL && head->val == val) { head = head->next; } // 定义两个指针,分别指向当前节点和前一个节点 struct ListNode *curr = head; struct ListNode *prev = NULL; // 遍历链表删除等于 val节点 while (curr != NULL) { if (curr->val == val) { // 如果当前节点等于 val删除当前节点 if (prev != NULL) { prev->next = curr->next; } free(curr); curr = prev->next; } else { // 如果当前节点不等于 val,继续遍历 prev = curr; curr = curr->next; } } return head; } int main() { // 创建链表 struct ListNode *head = (struct ListNode*)malloc(sizeof(struct ListNode)); head->val = 1; head->next = (struct ListNode*)malloc(sizeof(struct ListNode)); head->next->val = 2; head->next->next = (struct ListNode*)malloc(sizeof(struct ListNode)); head->next->next->val = 3; head->next->next->next = (struct ListNode*)malloc(sizeof(struct ListNode)); head->next->next->next->val = 2; head->next->next->next->next = NULL; // 删除链表等于给定 val 的所有节点 int val = 2; head = removeElements(head, val); // 输出删除后的链表 struct ListNode *curr = head; while (curr != NULL) { printf("%d ", curr->val); curr = curr->next; } printf("\n"); return 0; } ``` 注:这段代码使用了链表的基本操作,包括创建链表、遍历链表删除节点等。其删除链表等于给定 val 的所有节点的函数 removeElements 的实现思路是:先处理头节点,再遍历链表删除等于 val节点

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值