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

什么是链表?

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

在这里插入图片描述

单链表的基础与构造方法

单链表结构图

在这里插入图片描述

单列表创建

    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 + "} ");
      }
  }

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

猜你喜欢

转载自blog.csdn.net/qq_43305829/article/details/131753584
今日推荐