为什么两段代码,只改了一点命名为什么一个可以输出数据,一个却不行



package DateStructuresHomework;

public class S2 {
    public int no;
    public S2 next;

    public S2(int no) {
        this.no = no;
    }
    @Override
    public String toString() {
        return ""+no;

    }
}
class SingleLinkedList3
{
    private S2 head = new S2(0);
    public S2 getHead() {
        return head;
    }

    public void add(S2 S2) {
        //因为head节点不能动,因此我们需要一个辅助遍历 temp
        S2 temp = head;
        //遍历链表,找到最后
        while(true) {
            //找到链表的最后
            if(temp.next == null) {
                break;
            }
            //如果没有找到最后, 将将temp后移
            temp = temp.next;
        }
        //当退出while循环时,temp就指向了链表的最后
        //将最后这个节点的next 指向 新的节点
        temp.next = S2;
    }
    public void list() {
        //判断链表是否为空
        if(head.next == null) {
            System.out.println("链表为空");
            return;
        }
        //因为头节点,不能动,因此我们需要一个辅助变量来遍历
        S2 temp = head.next;
        while(true) {
            //判断是否到链表最后
            if(temp == null) {
                break;
            }
            //输出节点的信息
            System.out.println(temp);
            //将temp后移, 一定小心
            temp = temp.next;
            //temp=temp.next是使temp指向下一结点。
            //temp.next=temp是使temp本身的next指针指向自己。
        }
    }


}
class TEST2{
    public static void main(String[] args) {
        S2 a = new S2(1);
        SingleLinkedList3 singleLinkedList = new SingleLinkedList3();
        singleLinkedList.add(a);
        singleLinkedList.list();
    }
}
package DateStructuresHomework;

public class Node {
    public int val;
    public Node next;

    public Node(int val) {
        this.val = val;
    }
    @Override
    public String toString()
    {
        return ""+val;
    }
}


class SingleLinkedList
{
    private Node head=new Node(0);
    public Node getHead(){
        return head;
    }

    public void insert(Node Node)
    {
        Node temp=head;
        while (true)
        {
            if (temp.next==null)
            {
                    break;
            }
            temp=temp.next;

        }
        temp.next=Node;


        }

        public void display()
        {
            if (head.next==null)
            {
                System.out.println("空");
                return;
            }

            Node temp=head.next;
            while (true)
            {
                if (temp==null)
                    break;
            }
            System.out.println(temp);
            temp=temp.next;
        }





    }






    class Test5 {
        public static void main(String[] args) {
            Node node1 = new Node(1);
            SingleLinkedList singleLinkedList = new SingleLinkedList();
            singleLinkedList.insert(node1);
            singleLinkedList.display();


        }
    }



 
public class Node {
    public int val;
    public Node next;
 
    public Node(int val) {
        this.val = val;
    }
    @Override
    public String toString()
    {
        return ""+val;
    }
}
 
 
class SingleLinkedList
{
    private Node head=new Node(0);
    public Node getHead(){
        return head;
    }
 
    public void insert(Node Node)
    {
        Node temp=head;
        while (true)
        {
            if (temp.next==null)
            {
                    break;
            }
            temp=temp.next;
 
        }
        temp.next=Node;
 
 
        }
 
        public void display()
        {
            if (head.next==null)
            {
                System.out.println("空");
                return;
            }
 
            Node temp=head.next;
            while (true)
            {
                if (temp == null){
                    break;
                } else {
                    System.out.println(temp);
                }
                temp=temp.next;
            }

        }
    }
 
 
 
 
 
 
    class Test5 {
        public static void main(String[] args) {
            Node node1 = new Node(1);
            SingleLinkedList singleLinkedList = new SingleLinkedList();
            singleLinkedList.insert(node1);
            singleLinkedList.display();
 
 
        }
    }
 
 
 

while (true)
{
if (temp==null)
break;
}
这里干循环,输出写循环外面去了