从单向链表中删除指定值的节点

本文介绍了一种在单向链表中删除特定值节点的算法,并提供了完整的Java实现代码。文章详细阐述了如何构建链表及如何进行节点的插入与删除操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述

输入一个单向链表和一个节点的值,从单向链表中删除等于该值的节点,删除后如果链表中无节点则返回空指针。
链表结点定义如下:
struct ListNode
{
      int       m_nKey;
      ListNode* m_pNext;
};
详细描述:
本题为考察链表的插入和删除知识。
链表的值不能重复
构造过程,例如
1 -> 2
3 -> 2
5 -> 1
4 -> 5
7 -> 2
最后的链表的顺序为 2 7 3 1 5 4
删除 结点 2
则结果为 7 3 1 5 4

输入描述:

1 输入链表结点个数
2 输入头结点的值
3 按照格式插入各个结点
4 输入要删除的结点的值

输出描述:

输出删除结点后的序列

输入例子:

5
2
3 2
4 3
5 2
1 4
3

输出例子:

2 1 5 4

算法实现

import java.util.List;
import java.util.Scanner;

/**
 * Declaration: All Rights Reserved !!!
 */
public class Main {
    private static class ListNode {
        int key;
        ListNode next;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
//        Scanner scanner = new Scanner(Main.class.getClassLoader().getResourceAsStream("data.txt"));
        while (scanner.hasNextLong()) {
            int num = scanner.nextInt();
            int h = scanner.nextInt();
            ListNode head = new ListNode();
            head.key = h;

            for (int i = 0; i < num - 1; i++) {
                int newVal = scanner.nextInt();
                int afterVal = scanner.nextInt();
                addNode(newVal, afterVal, head);
            }

            int del = scanner.nextInt();

            head = delete(del, head);
            System.out.println(getString(head));

        }
        scanner.close();
    }

    private static String getString(ListNode head) {
        StringBuilder builder = new StringBuilder();

        while (head!= null) {
            builder.append(head.key).append(' ');
            head = head.next;
        }

//        return builder.substring(0, builder.length() - 1);
        return builder.toString();
    }

    private static void addNode(int newVal, int afterVal, ListNode head) {
        ListNode n = head;
        while (n != null) {
            if (n.key == afterVal) {
                ListNode node = new ListNode();
                node.key = newVal;
                node.next = n.next;
                n.next = node;
                break;
            }
            n = n.next;
        }
    }

    private static ListNode delete(int val, ListNode head) {
        if (head.key == val) {
            ListNode ret = head.next;
            head.next = null;
            return ret;
        } else {
            ListNode prev = head;
            while (prev.next != null) {
                if (prev.next.key == val) {
                    prev.next = prev.next.next;
                    break;
                }
                prev = prev.next;
            }

            return head;
        }
    }
}

转载于:https://my.oschina.net/u/2822116/blog/826960

### 如何在 Java 中从单向链表删除指定节点 为了实现在 Java 单向链表中移除特定数节点,可以采用遍历链表并比较每个节点来找到目标节点。一旦找到了该节点,则调整指针绕过它。 下面是一个具体的实现方法: #### 定义 ListNode 类 首先定义一个 `ListNode` 类表示链表中的节点结构[^3]。 ```java class ListNode { int val; ListNode next; ListNode(int x) { val = x; next = null; } } ``` #### 删除操作函数 接着编写用于删除具有给定的第一个匹配项的方法。此过程涉及检查头部节点以及后续节点的情况。 如果要删除的是头结点,则直接更新头指针;对于其他位置上的节点,在前驱节点处改变其 `next` 字段即可完成删除动作[^1]。 ```java public class LinkedListUtil { public static void removeValue(ListNode head, int targetVal) { // 如果头节点就是要找的目标则移动head到下一个节点直到不是targetVal为止 while (head != null && head.val == targetVal) { head = head.next; } // 创建一个新的临时变量current用来保存当前处理的位置 ListNode current = head; // 遍历整个列表寻找等于目标的元素 while (current != null && current.next != null) { if (current.next.val == targetVal) { // 当发现下一个是待删节点时跳过之 current.next = current.next.next; } else { // 否则继续前进 current = current.next; } } } } ``` 上述代码实现了当遇到目标相等的节点时将其从链表中断开的功能,并且能够正确处理多个连续相同的情形。需要注意的是这里假设输入的有效性和合法性已经得到了保证。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值