算法通过村第一期--青铜挑战

链表青铜挑战


链表的创建

常用的链表创建结构

public class ListNode {
    public int val;//val用来存储数据
    public ListNode next;//指向下一个节点,是把节点连起来的基础

    public ListNode(int x) {//构造函数
        val = x;
        next = null;
    }

一个简单的链表实例

package com.yugutou.charpter1_linklist.level1;

/**
 * 一个简单的链表实例,用于演示JVM怎么构造链表的
 */
public class BasicLink {

    public static void main(String[] args) {
        int[] a = {1, 2, 3, 4, 5, 6};
        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;
        }
    }
}

通过debug可以看出该链表的数据结构,val存储当前数据,next指向下一个节点
在这里插入图片描述


链表的增删

遍历链表

**链表遍历原理:**从头开始逐向后访问
在这里插入图片描述

代码如下(示例):

    /**
     * 获取链表长度
     *
     * @param head 链表头节点
     * @return 链表长度
     */
    public static int getLength(Node head) {
        int length = 0;//获取链表长度
        Node node = head;
        while (node != null) {
            length++;
            node = node.next;
        }
        return length;
    }

插入链表

1.表头插入: 执行newNode.next = head即可,但要注意头节点要重新指向表头在这里插入图片描述

2.表中插入: 找到要插入的两个节点之间,前一个节点处停下来,如图,执行new.next = node(15.next),然后执行node(15).next = new,且顺序不能颠倒如果颠倒的话会导致15和7的联系断开,new就无法找到7了
在这里插入图片描述

**3.表尾插入:**直接将尾节点指向新节点
在这里插入图片描述

下面是一个链表插入的例子

 /**
     * 链表插入
     *
     * @param head       链表头节点
     * @param nodeInsert 待插入节点
     * @param position   待插入位置,取值从2开始
     * @return 插入后得到的链表头节点
     */
    public static Node insertNode(Node head, Node nodeInsert, int position) {
        // 需要判空,否则后面可能会有空指针异常
        if (head == null) {
            return nodeInsert;
        }
        //越界判断
        int size = getLength(head);
        if (position > size + 1 || position < 1) {
            System.out.println("位置参数越界");
            return head;
        }

        //在链表开头插入
        if (position == 1) {
            nodeInsert.next = head;
//            return nodeInsert;
            //上面return还可以这么写:
            head = nodeInsert;
            return head;
        }

        Node pNode = head;
        int count = 1;
        while (count < position - 1) {
            pNode = pNode.next;
            count++;
        }
        nodeInsert.next = pNode.next;
        pNode.next = nodeInsert;

        return head;
    }

该处使用的url网络请求的数据。


删除链表

1.删除表头节点: 执行head = head.next即可
在这里插入图片描述
2.删除中间按节点: 将cur.next的位置变成cur.next.next
在这里插入图片描述
3.删除尾节点: 前一个位置cur.next = null即可
在这里插入图片描述

删除代码示例

    /**
     * 删除节点
     *
     * @param head     链表头节点
     * @param position 删除节点位置,取值从1开始
     * @return 删除后的链表头节点
     */
    public static Node deleteNode(Node head, int position) {
        if (head == null) {
            return null;
        }
        int size = getLength(head);
        //思考一下,这里为什么是size,而不是size+1
        if (position > size || position <1) {
            System.out.println("输入的参数有误");
            return head;
        }
        if (position == 1) {
            //curNode就是链表的新head
            return head.next;
        } else {
            Node cur = head;
            int count = 1;
            while (count < position - 1) {
                cur = cur.next;
                count++;
            }
            Node curNode = cur.next;
            cur.next = curNode.next;
        }
        return head;
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值