链表问题.

单链表

单链表中有多个节点,每个节点包括两个属性,一个是当前值(value),另一个是指向一个节点的指针(next)。

30f2e8731ec0493c8b0c7713e55a5308.png

public class Node {
    int value;
    Node next;

    public Node() {
    }

    public Node(int value, Node next) {
        this.value = value;
        this.next = next;
    }
    
    public Node(int value) {
        this.value = value;
    }

    public Node(Node next) {
        this.next = next;
    }
}

添加元素

1.头部插入

创建一个新节点newNode,并将next指向头节点head(newNode.next = head),最后将newNode赋值给head,使head继续指向链表中第一个节点。

b739ed3a8b74485581672d1f5786429c.png

 public Node insertNode(Node head, Node nodeInsert) {
        if (head == null) { //判断链表是否为空
            return nodeInsert;
        }
        nodeInsert.next = head;
        head = nodeInsert;
        return nodeInsert;
}

 2.中间插入

首先要找到插入的位置,当前节点的下一个节点(cur.next)为新节点插入的位置,先将新节点(newNode)指向当前节点的下一个节点(newNode.next = cur.next),然后再将当前节点(cur)的下一个节点指向新节点(cur.next = newNode)

65858a0591514d858e77e39d87b74f3e.png

public Node insertNode(Node head, Node nodeInsert, int position) {
        // 链表是否为空
        if (head == null) {
            return null;
        }
        // 判断是否越界
        int size = getLength(head);
        if (position < 1 || position > size + 1) {
            return null;
        }
        // 找到插入位置
        Node cur = head;
        int count = 1;
        while (count < position - 1) {
            count++;
            cur = cur.next;
        }
        // 插入结点
        nodeInsert.next = cur.next;
        cur.next = nodeInsert;
        return head;
    }

 3.尾部插入

找到最后一个节点,并将它指向新节点(last.next = newNode),最后将新节点指向null(newNode.next = null).

8669e8130c174c5ebe8e418e65a274a6.png

    public  Node insertIntoTail(Node head, Node newNode) {
        //判断链表是否为空
        if (head == null) {
            return null;
        }
        Node last = head;
        //找到最后一个节点
        while (last.next != null) {
            last = last.next;
        }
        last.next = newNode;
        newNode.next = null;
        return head;
    }

 链表删除

1.头部删除

将头节点(head)指向下一个节点(head = head.next)。

d1e437414f1e4f1d9a1406019e06a960.png

 public Node insertNode(Node head) {
        if (head == null) { //判断链表是否为空
            return nodeInsert;
        }
        head = head.next;
        return head;
}

2.中间删除

先找到要删除节点的上一个节点(cur),将这个节点指向要删除节点的下一个节点(cur.next = cur.next.next)进行删除

527af28e8cf34459bd72fae414364755.png

public Node insertNode(Node head, Node nodeInsert, int position) {
        // 链表是否为空
        if (head == null) {
            return null;
        }
        // 判断是否越界
        int size = getLength(head);
        if (position < 1 || position > size + 1) {
            return null;
        }
        // 找到删除位置
        Node cur = head;
        int count = 1;
        while (count < position - 1) {
            count++;
            cur = cur.next;
        }
        // 删除结点
        cur.next = cur.next.next;
        return head;
    }

 3.尾部删除

找到倒数第二个节点(cur.next.next == null)并将它指向null(cur.next = null)

36bd24dd4b8d4930a3db877ee8e525a8.png

    public  Node insertIntoTail(Node head) {
        //判断链表是否为空
        if (head == null) {
            return null;
        }
        Node last = head;
        //找到最后一个节点
        while (last.next.next != null) {
            last = last.next;
        }
        last.next = null;
        return head;
    }

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值