当前位置:首页 > Java

如何用java实现链表

2026-03-18 17:32:12Java

实现链表的基本结构

链表由节点(Node)组成,每个节点包含数据(data)和指向下一个节点的引用(next)。

class Node<T> {
    T data;
    Node<T> next;

    public Node(T data) {
        this.data = data;
        this.next = null;
    }
}

定义链表类

链表类需要管理节点的增删改查操作,并维护头节点(head)。

class LinkedList<T> {
    private Node<T> head;

    public LinkedList() {
        this.head = null;
    }
}

插入节点到链表头部

将新节点插入链表头部,并更新头节点指针。

如何用java实现链表

public void insertAtHead(T data) {
    Node<T> newNode = new Node<>(data);
    newNode.next = head;
    head = newNode;
}

插入节点到链表尾部

遍历链表找到最后一个节点,将新节点链接到末尾。

public void insertAtTail(T data) {
    Node<T> newNode = new Node<>(data);
    if (head == null) {
        head = newNode;
        return;
    }
    Node<T> current = head;
    while (current.next != null) {
        current = current.next;
    }
    current.next = newNode;
}

删除指定值的节点

遍历链表找到目标节点,调整前驱节点的next指针以跳过目标节点。

如何用java实现链表

public void delete(T data) {
    if (head == null) return;
    if (head.data.equals(data)) {
        head = head.next;
        return;
    }
    Node<T> current = head;
    while (current.next != null && !current.next.data.equals(data)) {
        current = current.next;
    }
    if (current.next != null) {
        current.next = current.next.next;
    }
}

查找节点是否存在

遍历链表,检查是否存在与目标值匹配的节点。

public boolean contains(T data) {
    Node<T> current = head;
    while (current != null) {
        if (current.data.equals(data)) {
            return true;
        }
        current = current.next;
    }
    return false;
}

打印链表内容

遍历链表并依次输出每个节点的值。

public void printList() {
    Node<T> current = head;
    while (current != null) {
        System.out.print(current.data + " -> ");
        current = current.next;
    }
    System.out.println("null");
}

示例用法

创建一个链表并测试插入、删除和查找功能。

public class Main {
    public static void main(String[] args) {
        LinkedList<Integer> list = new LinkedList<>();
        list.insertAtHead(3);
        list.insertAtHead(2);
        list.insertAtHead(1);
        list.insertAtTail(4);
        list.printList(); // 输出: 1 -> 2 -> 3 -> 4 -> null

        list.delete(2);
        list.printList(); // 输出: 1 -> 3 -> 4 -> null

        System.out.println("Contains 3: " + list.contains(3)); // 输出: true
    }
}

标签: 如何用链表
分享给朋友:

相关文章

php 链表实现

php 链表实现

在 PHP 中实现链表可以通过类来模拟链表节点(Node)和链表(LinkedList)结构。以下是链表的完整实现示例,包含基础操作如插入、删除、遍历等。 链表节点类(Node) 链表的基本单元是节…

如何用java画图

如何用java画图

使用 Java 绘图基础 Java 提供了多种绘图方式,主要通过 java.awt 和 javax.swing 包中的类实现。以下是几种常见的绘图方法。 使用 Graphics 类绘图 Graphi…

react hooks如何用

react hooks如何用

使用 React Hooks 的基本方法 React Hooks 是 React 16.8 引入的功能,允许在函数组件中使用状态和其他 React 特性。以下是几种常用 Hooks 的用法。 use…

如何用react写页面

如何用react写页面

创建React项目 使用create-react-app快速初始化项目,需提前安装Node.js环境。运行命令: npx create-react-app my-app cd my-app npm…

如何用vs code开发react

如何用vs code开发react

安装必要工具 确保系统已安装Node.js(建议LTS版本),用于运行JavaScript和npm/yarn包管理工具。下载并安装Visual Studio Code(VS Code)作为代码编辑器。…

如何用vscode 启动react项目

如何用vscode 启动react项目

安装必要工具 确保已安装Node.js(包含npm或yarn),可在终端运行node -v和npm -v检查版本。未安装需从Node.js官网下载。 创建React项目 打开VSCode终端(快捷键…