欢迎访问宙启技术站
智能推送

Java中实现链表的增删改查操作

发布时间:2023-06-25 15:50:57

链表是一种常见的数据结构,它由多个节点组成,节点之间通过指针相互连接。在Java中,实现链表可以使用如下的方式:

1.定义节点类

链表的节点类一般包含两个属性:数据域和指针域。数据域用来存储数据,指针域用来指向下一个节点。

public class ListNode {
    int val;
    ListNode next;
    ListNode(int x) { val = x; }
}

2.实现增加节点的操作

链表增加节点的操作一般分为三种情况:在链表头部增加、在链表尾部增加和在链表中间插入。

在链表头部增加节点的操作,可以使用以下的代码实现:

public ListNode addAtHead(ListNode head, int val) {
    ListNode node = new ListNode(val);
    node.next = head;
    head = node;
    return head;
}

在链表尾部增加节点的操作,可以使用以下的代码实现:

public ListNode addAtTail(ListNode head, int val) {
    ListNode node = new ListNode(val);
    if(head == null) {
        head = node;
    } else {
        ListNode cur = head;
        while(cur.next != null) {
            cur = cur.next;
        }
        cur.next = node;
    }
    return head;
}

在链表中间插入节点的操作,可以使用以下的代码实现:

public ListNode addAtIndex(ListNode head, int index, int val) {
    if(index <= 0) {
        ListNode node = new ListNode(val);
        node.next = head;
        head = node;
    } else {
        ListNode pre = head;
        ListNode cur = pre.next;
        for(int i = 1; i < index && cur != null; i++) {
            pre = cur;
            cur = cur.next;
        }
        if(cur == null) {
            ListNode node = new ListNode(val);
            pre.next = node;
        } else {
            ListNode node = new ListNode(val);
            pre.next = node;
            node.next = cur;
        }
    }
    return head;
}

3.实现删除节点的操作

链表删除节点的操作一般也分为三种情况:删除链表头部节点、删除链表尾部节点和删除链表中间节点。

删除链表头部节点的操作,可以使用以下的代码实现:

public ListNode deleteAtHead(ListNode head) {
    if(head == null) {
        return null;
    }
    ListNode temp = head;
    head = head.next;
    temp.next = null;
    return head;
}

删除链表尾部节点的操作,可以使用以下的代码实现:

public ListNode deleteAtTail(ListNode head) {
    if(head == null || head.next == null) {
        return null;
    }
    ListNode pre = head;
    ListNode cur = head.next;
    while(cur.next != null) {
        pre = cur;
        cur = cur.next;
    }
    pre.next = null;
    return head;
}

删除链表中间节点的操作,可以使用以下的代码实现:

public ListNode deleteAtIndex(ListNode head, int index) {
    if(head == null) {
        return null;
    }
    if(index == 0) {
        ListNode temp = head;
        head = head.next;
        temp.next = null;
    } else {
        ListNode pre = head;
        ListNode cur = pre.next;
        for(int i = 1; i < index && cur != null; i++) {
            pre = cur;
            cur = cur.next;
        }
        if(cur != null) {
            pre.next = cur.next;
            cur.next = null;
        }
    }
    return head;
}

4.实现查找节点的操作

链表查找节点的操作一般包括查找链表中节点的个数和查找节点的值。

查找链表中节点的个数的操作,可以使用以下的代码实现:

public int countNodes(ListNode head) {
    int count = 0;
    ListNode cur = head;
    while(cur != null) {
        count++;
        cur = cur.next;
    }
    return count;
}

查找节点的值的操作,可以使用以下的代码实现:

public boolean searchNode(ListNode head, int val) {
    ListNode cur = head;
    while(cur != null) {
        if(cur.val == val) {
            return true;
        }
        cur = cur.next;
    }
    return false;
}

综上所述,Java中实现链表的增删改查操作可以分为四个部分:定义节点类、实现增加节点的操作、实现删除节点的操作和实现查找节点的操作。在实际开发过程中,需要根据具体的需求进行相应的操作实现。