简体   繁体   English

如果我们检查 head==null 为什么不在 Java 链表中检查 tail==null 呢?

[英]If we check head==null why don't we check tail==null in Java linkedlist?

I am a beginner in Java and currently completing a Udemy course on DSA.我是 Java 的初学者,目前正在完成有关 DSA 的 Udemy 课程。 I am learning linked lists and am working on methods to insert and delete nodes to and from linked lists respectively.我正在学习链表,并且正在研究分别在链表中插入和删除节点的方法。

From what I have learnt so far I know that we use condition head==null to check if the linked list is empty or not.根据我目前所学的知识,我知道我们使用条件head==null来检查链表是否为空。

If the condition head==null is true then LinkedList is empty else it is not empty.如果条件head==null为真,则 LinkedList 为空,否则不为空。

However, shouldn't we check whether tail==null as well because the tail will always refer to the last node in the LinkedList even after we make head==null ?但是,我们不应该检查是否tail==null ,因为即使在我们 make head==null之后,tail 也会始终引用 LinkedList 中的最后一个节点?

Here is my code:这是我的代码:

public class SinglyLinkedList{
  public Node head;
  public Node tail;
  public int size;

//Create Linkedlist
  public Node createLL(int num){
    Node node=new Node();
    node.value=num;
    node.next=null;
    head=node;
    tail=node;

    size=1;
    return head;
  }
//Insert Node
  public void insertNode(int num,int location){
    Node node=new Node();
    node.value=num;
    
    if(head==null){//Used to check if linked list is empty or not?
      createLL(num);
      return;
    }

    else if(location==0){
      node.next=head;
      head=node;
    }

    else if(location>=size){
      node.next=null;
      tail.next=node;
      tail=node;
    }

    else{
      Node tempNode=head;
      int index=0;

      while(index<location-1){
        tempNode=tempNode.next;
        index++;
      }
     node.next=tempNode.next;
     tempNode.next=node;
    }
    size++;
  }
//Delete Node
  public void deleteNode(int location){
    if(head==null){//Used to check if linked list is empty or not?
      System.out.println("The linked list is not present");
      return;
    }

    else if(location==0){
      head=head.next;
      size--;
      if(size==0){
        tail=null;
      }
    }

    else if(location>=size){
      Node tempNode=head;
      for(int i=0;i<size-1;i++){
        tempNode=tempNode.next;
      }
      if(head==null){
        tail=null;
        size--;
        return;
      }
      tempNode.next=null;
      tail=tempNode;
      size--;
    }

    else{
      Node tempNode=head;
      int index=0;

      while(index<location-1){
        tempNode=tempNode.next;
        index++;
      }
      tempNode.next=tempNode.next.next;
      size--;
    }
  }
}

Empty list:空列表:

head -> null
tail -> null

Single node list:单节点列表:

head -> node1
node1.next -> null
tail -> node1

Multi node list:多节点列表:

head -> node1
node1.next -> node2
node2.next -> null
tail -> node2

Where -> means "reference points to".其中->表示“参考点”。 So there's no need to check for both head/tail for nullness.所以没有必要同时检查头部/尾部是否为空。 If either of them is null, it means the list has no nodes and is empty.如果其中任何一个是 null,则表示列表没有节点并且为空。

Nodes in a Linked-List exist as a value and memory address (for the next node) pair as you have implemented in the program above.正如您在上面的程序中实现的那样,链接列表中的节点作为值存在,并且 memory 地址(用于下一个节点)对。 For the last node, the end of the list is represented as NULL.对于最后一个节点,列表的末尾表示为 NULL。

We check if the head is NULL since if the memory address of the next node in the linked list is NULL, then that means the end of the list has been reached.我们检查头部是否是NULL,因为如果链表中下一个节点的memory地址是NULL,则表示已经到达链表的末尾。 The tail of the linked list will always be NULL since it would represent the end of the list, and there would be no next node to point to.链表的尾部将始终是 NULL,因为它代表链表的结尾,并且没有下一个节点可以指向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM