算法通关村第一关——链表青铜挑战笔记

本项目地址  https://gitee.com/linxux001/algorithm-village

理解java是如何构造出链表的(单链表)

1.链表的创建:
链表表示的是一列元素,是一种递归的数据结构(递归是指在函数的定义中使用函数自身的方法),
用一个嵌套类来定义结点的抽象数据类型(嵌套类是指在一个类中定义另一个类,此处为静态嵌套类,通过对象的引用来使用,例如Node.next):

private class Node{
	int data;  //可以使用泛型
	Node next; //递归
	public Node(int data){ this.data=data; } //给结点赋值
}

或规范的定义:

private class Node{
	int data;  //可以使用泛型
	Node next; //递归
	public Node(int data){ this.data=data; } //给结点赋值
	public int getData() {
        return data;
    }
    public void setData(int data) {
        this.data = data;
    }
    public Node getNext() {
        return next;
    }
    public void setNext(Node next) {
        this.next = next;
    }
}

2.通过数组初始化链表:

private static Node initLinkedList(int[] array){
		Node head = null, cur = null; //定义头结点和当前结点
        for (int i = 0; i < array.length; i++) {
            //创建新结点,结点的值赋为i
            Node newNode = new Node(array[i]);
            newNode.next = null;
            //索引i=0时 head和cur同时为首结点
            if (i == 0) {
                head = newNode;
                cur = newNode;
            } else {
                cur.next = newNode;
                cur = newNode;
            }
        }
        return head;//返回头结点
}

3.遍历链表

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

4.链表插入
仔细分析可以从表头、中间和表尾入手

 	/**
     * 链表插入
     *
     * @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;
    }

5.链表删除

	/**
     * 删除节点
     *
     * @param head     链表头节点
     * @param position 删除节点位置,取值从1开始
     * @return 删除后的链表头节点
     */
    public static Node deleteNode(Node head, int position) {
        if (head == null) {
            return null;
        }
        int size = getLength(head);
        if (position > size || position <= 0) {
            System.out.println("输入的参数有误");
            return head;
        }

        if (position == 1) {
            // head.next就是链表的新head
            return head.next;
        }
        Node preNode = head;
        int count = 1;
        while (count < position) {
            preNode = preNode.next;
            count++;
            Node curNode = preNode.next;
            preNode.next = curNode.next;
        }
        return head;
    }

5.总结:算法的基础是数据结构,任何数据结构的基础都是创建与增删改查,作为很多算法题的内容。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值