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

什么是链表?

链表是一种比较常见的数据结构,链表由多个结构相同的节点连接而成,每个节点分为数据域指针域,数据域存放数据,指针域存放下一个节点的地址。
在这里插入图片描述

在这里插入图片描述

单链表的基础与构造方法

单链表结构图

在这里插入图片描述

单列表创建

    public class Node {
        public int val;

        public Node next;

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

单列表初始化

    static Node initLinkedList(int[] array) {
        //初始化head 都为null 保证内存地址一样,,
        Node head = null;
        Node currentNode = null; 
        for (int i = 0; i < array.length; i++) {
            Node newNode = new Node(array[i]);
            newNode.next = null;
            if (i == 0) {
               //初始化头节点
                head = newNode;
                currentNode = newNode;
            } else {
              //讲当前的节点的指针指向下一个节点
                currentNode.next = newNode;
                //将下一个节点赋值给当前节点
                currentNode = newNode;
            }
        }
        return head;
    }

获取列表的长度

  public   static  int getListLength(Node head){
        int length=0;
        Node node = head;
        while (node != null ){
            length++;
            node=node.next;
        }
        return length;
    }

链表插入元素

(1)在链表的表头插入

  1. 创建插入节点newNode
  2. newNode.next = head
  3. head = newNode

(2)在链表的中间插入

    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) {
          return head;
      }

      if (position == 1) {
          nodeInsert.next = head
          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;
  }

(3)在链表的尾部插入

链表删除

(1)删除表头节点

  1. 让头节点指向下一个元素
    head = head.next

(2)删除中间节点

(3)删除末尾节点

    public static Node deleteNode(Node head, int position) {
        if (head == null) {
            return null;
        }
        int size = getLength(head);
        if (position > size || position <= 0) {
            return head;
        }
        if (position == 1) {
            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;
    }

双向链表的定义

    class DoubleNode {
      public int data;    //数据域
      public DoubleNode next;    //指向下一个结点
      public DoubleNode prev;
      public DoubleNode(int data) {
          this.data = data;
      }
      //打印结点的数据域
      public void displayNode() {
          System.out.print("{" + data + "} ");
      }
  }

结构图:
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值