算法村第一关 青铜挑战 | 链表的创建及增删改查

1. 单链表的定义

每个结点由两部分组成,自身的值val,和下一结点的地址next,当next为空时,即为尾结点。

区分以下链表是否为单链表👇:

图1是满足单链表要求的。一个结点可以同时被两个或者多个结点指向,但只能有一个后继结点,而图2的c1结点分出两个岔,就不再符合要求。

2. 链表的创建、遍历

2.1 链表创建

在Java中的规范创建方式:

public class ListNode{
    
    private int val;
    private ListNode next;

    public ListNode(int val) {
        this.val = val;
    }

    public int getVal() {
        return val;
    }

    public void setVal(int val) {
        this.val = val;
    }

    public ListNode getNext() {
        return next;
    }

    public void setNext(ListNode next) {
        this.next = next;
    }
}

在LeetCode中采用的创建方式(本系列统一采用此方式)

public class ListNode{
    
    public int val;
    public ListNode next;

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

    public static void main(String[] args) {
        ListNode listnode = new ListNode(1);
    }
}

debug如下代码,链表就是从head开始,逐个向后访问,val依次为1 3 5 7 9

public class MyLinkList {
    
    public static void main(String[] args) {
        int[] a = {1, 3, 5, 7, 9};
        Node head = initLinkedList(a);
        System.out.println(head);
    }

    /**
     * 初始化链表
     */
    private static Node initLinkedList(int[] array) {
        Node head = null, cur = null;
        for (int i = 0; i < array.length; i++) {
            Node newNode = new Node(array[i]);
            newNode.next = null;
            if (i == 0) {
                head = newNode;
                cur = newNode;
            } else {
                cur.next = newNode;
                cur = newNode;
            }
        }
        return head;
    }

    /**
     * 创建结点
     */
    static class Node {
        public int val;
        public Node next;

        Node(int x) {
            val = x;
            next = null;
        }
    }
}

2.2 链表遍历

// 返回已存放元素的个数,即当前链表的长度
public static int getListLength(Node head) {
    int length = 0;
    // 从头开始遍历
    Node node = head;
    while (node != null) {
        length++;
        // 往后移一位
        node = node.next;
    }
    return length;
 }

3. 链表的增删改查

3.1 链表插入

上图为链表的插入,分头部、尾部、中间三种情况讨论,箭头所示分别用红色、橙色、黄色表示。

在头部,新结点的next指向现有的head结点,再将新结点表示为head;

在尾部,新结点的next指向null,成为尾结点,再将旧的结点prev指向新结点;

在中间,新结点的next指向旧结点的next,不致信息丢失,再将旧结点prev指向新结点。

综合代码如下👇:

/**
 * 链表插入
 *
 * @param head       链表头结点
 * @param newNode    待插入结点
 * @param position   待插入位置,取值从1开始
 * @return 插入后得到的链表头结点
 */
public static Node insertNode(Node head, Node newNode, int position){
    if (head == null) {
        // 没有头结点,新结点就是头
        return newNode;
    }
    
    // 获取当前链表长度
    int size = getLength(head);
    // 校验插入位置,最大是链表长度+1,最小是1
    if (position > size + 1 || position < 1) {
        System.out.println("插入位置越界!");
        return head;
    }
    
    // 表头插入
    if (position == 1) {
        // 图示操作
        newNode.next = head;
        return newNode; // 这里替代了 head = newNode; return head; 的写法
    }
    // 尾部或中间插入,可以合并在一起,找到前一位置结点prevNode
    Node prevNode = head;
    int count = 1;
    // 直到找到前一位置
    while (count < position -1) {
        prevNode = prevNode.next;
        count++;
    }
    // 操作,先连上下一个结点
    newNode.next = prevNode.next;
    prevNode.next = newNode;
    
    return head;
}

3.2 链表删除

上图为链表的删除,分头部、尾部、中间三种情况讨论,箭头所示分别用红色、橙色、黄色表示。

在头部,头结点head直接指向下一个结点,如同丢弃了原有的头结点;

在尾部,尾结点的上一结点cur的next指向null,如同丢弃了原有的尾结点;

在中间,待删除的上一结点cur的next指向cur.next.next,如同丢弃了待删除结点。

综合代码如下👇:

/**
 * 删除结点
 *
 * @param head     链表头结点
 * @param position 删除结点位置,取值从1开始
 * @return 删除后的链表头结点
 */
public static Node deleteNode(Node head, int position){
    if (head == null) {
        // 没有头结点,删不了
        return null;
    }
    
    // 获取当前链表长度
    int size = getLength(head);
    // 校验删除位置,最大是链表长度,最小是1
    if (position > size || position < 1) {
        System.out.println("删除位置不存在!");
        return head;
    }
    
    // 表头删除
    if (position==1) {
        return head.next;
    } else {
    // 尾部或中间删除,先找到当前位置cur
    Node cur = head;
    int count = 1;
    // 直到找到当前位置
    while (count < position - 1) {
        cur = cur.next;
        count++;
    }
    // 操作
    cur.next = cur.next.next;
    }
    return head;
}

4. 总结

本文介绍了链表(主要是单链表)的定义,链表的创建(LeetCode写法)和遍历方式,以及讨论了结点插入和删除的三种情况。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值