Link List Learning--Implementation of Single Link List-Addition and Deletion Checking

Posted by mikie46 on Mon, 07 Oct 2019 01:08:22 +0200

Linked list

  • In most cases, we will use the header node (the first node) to represent the entire list.
  • If we want to get the first element, we have to go through it one by one from the beginning. The average O(N) time we spend accessing elements by index, where N is the length of the list.

Implement the following functions:

  • get(index): Gets the value of the index node in the list. If the index is invalid, return - 1.
  • addAtHead(val): Add a node with value Val before the first element of the list. After insertion, the new node will become the first node in the list.
  • addAtTail(val): Appends a node with value to the last element of the list.
  • addAtIndex(index,val): Add a node with a value of Val before the index node in the list. If index equals the length of the list, the node is appended to the end of the list. If index is larger than the list length, no nodes are inserted. If index is less than 0, the node is inserted in the head.
  • Delete at index (index): if the index index is valid, delete the index node in the linked list.

The code is as follows:

1. Defining the nodes of a single linked list

     package com.leetCode.study.Demo_2019_8_8_Linked;
          /**
           * Definition of single linked list
           * @author liudongting
           * @date 2019/8/8 10:45
           */
          public class SinglyListNode {
          
          
              //val is the value of the current node
          
              int val;
          
              //Next is the pointer / reference to the next node
          
              SinglyListNode next;
              //Parametric constructor
          
              SinglyListNode(int x) { val = x; }
          
          }
 

Two, linked list

 package com.leetCode.study.Demo_2019_8_8_Linked;
 
 /**
  * @author liudongting
  * @date 2019/8/8 10:56
  */
 public class List {
 
     static  SinglyListNode head;
 
     /**
      *
      * addAtTail(val)
      * Append the node with val ue to the last element of the list.
      */
     public void addAtTail(int val){
         SinglyListNode singlyListNode = new SinglyListNode(val);
         //If the header node is empty, add it to the header node
         if(head == null){
             head = singlyListNode;
             return;
         }
         //Start from the scratch node
         SinglyListNode temp = head;
         //Judge from the beginning whether there is a next node and find the last one
         while (temp.next != null){
             temp = temp.next;
         }
         //The pointer to the last node points to the newly added node
         temp.next = singlyListNode;
     }
 
     /**
      * Gets the value of the index node in the list. If the index is invalid, return - 1.
      * @param index
      */
     public int get(int index){
 
       int length =0 ;
       //Start from the scratch node
       SinglyListNode temp = head;
         //If the node is not empty, determine whether the index of the node is consistent with the required index. The same goes back, the different goes on.
         while (temp != null) {
             if(length==index){
                 return temp.val;
             }
             length++;
             temp = temp.next;
         }
       return -1;
     }
 
     /**
      * Add a node with val ue before the first element of the list. After insertion, the new node will become the first node in the list.
      * @param val
      */
 
     public void  addAtHead(int val){
         SinglyListNode singlyListNode = new SinglyListNode(val);
         //If the header node is empty, add it to the header node
         if(head == null){
             head = singlyListNode;
             return;
         }
         //Create a temporary node to save the value of the header node
         SinglyListNode temp = head;
         //Value of the new node, to the header node
         head=singlyListNode;
         //The next element of the new header node points to the original header node.
          head.next = temp;
     }
 
     /**
      *  Add a node with a value of val before the index node in the list.
      *  If index equals the length of the list, the node is appended to the end of the list.
      *  If index is larger than the list length, no nodes are inserted. If index is less than 0, the node is inserted in the head.
      *
      *
      */
     public void addAtIndex(int index,int val){
         if(index<0){
             addAtHead(val);
             return;
         }
         if(linkListLength()==index){
             addAtTail(val);
             return;
         }
         if(index>linkListLength()){
             return;
         }
         SinglyListNode singlyListNode = new SinglyListNode(val);
         int length =0 ;
         //Start from the scratch node
         SinglyListNode temp = head;
         //If the node is not empty, determine whether the index of the node is consistent with the required index. The same goes back, the different goes on.
         while (temp != null) {
             if((length+1)==index){
                 //The node pointed by the pointer of the previous node of index, that is, the node at index
                 SinglyListNode preNode = temp.next;
                 //The pointer to the previous node of index points to the new node
                temp.next = singlyListNode;
                //The next pointer of the new node points to the index node
                singlyListNode.next= preNode;
                return;
             }
             length++;
             temp = temp.next;
         }
     }
 
     /**
      * Get the length of the linked list
      * @return
      */
     public int  linkListLength() {
 
         int length = 0;
 
         //Temporary node, starting from the first node
         SinglyListNode temp = head;
 
         // Find the tail node
         while (temp != null) {
             length++;
             temp = temp.next;
         }
 
         return length;
     }
 
     /**
      * If the index index is valid, the index node in the linked list is deleted.
      * @param index
      */
     public void deleteAtIndex(int index){
 
         int length = 0;
 
         //Temporary node, starting from the first node
         SinglyListNode temp = head;
         if(index==length){
            head=head.next;
         }
         // Find the tail node
         while (temp != null) {
             if((index-1)==length){
                 SinglyListNode indexNode =temp.next;
                 SinglyListNode nextNode = indexNode.next;
                 temp.next = nextNode;
                 return;
             }
             length++;
             temp = temp.next;
         }
 
     }
     public static void main(String[] args) {
         List list = new List();
         // Additive elements
         list.addAtTail(2);
         //Adding elements to the header node
         list.addAtHead(3);
         //Adding elements at specified locations
         list.addAtIndex(1,4);
         //Delete elements by index
         list.deleteAtIndex(2);
         //Get the length of the linked list
         list.linkListLength();
         System.out.println("222");
         System.out.println(" Get the length of the linked list list.linkListLength() : " +list.linkListLength());
         System.out.println(" Get an element of the list list.get(1) : " + list.get(2));
     }
 }

Welcome to the Public Number for more information:

Topics: Java less