アルゴリズム クリアランス ビレッジの最初のレベル - リンク リスト ブロンズ チャレンジ ノート

リンクリストとは何ですか?

リンク リストは比較的一般的なデータ構造です。リンク リストは同じ構造を持つ複数のノードで接続されています。各ノードはデータ フィールドポインタ フィールドに分かれています。データ フィールドにはデータが格納され、ポインタ フィールドにはデータのアドレスが格納されます。次のノード。
ここに画像の説明を挿入します

ここに画像の説明を挿入します

単結合リストの基本と構築方法

単一リンクリスト構造図

ここに画像の説明を挿入します

単一リストの作成

    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 = 頭
  3. head = 新しいノード

(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 = 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