【发布时间】:2021-09-04 14:32:56
【问题描述】:
我在 java 中的简单链表程序下面运行,但我得到了一个元素。
我得到的输出
10
8
1
public class SinglyLinkedList {
ListNode head;
private static class ListNode {
int data;
ListNode next;
public ListNode(int data) {
this.data=data;
this.next = null;
}
}
public void display() {
ListNode curentNode = head;
while (curentNode.next != null) {
System.out.println(curentNode.data);
curentNode = curentNode.next;
}
}
public static void main(String[] args) {
SinglyLinkedList sll = new SinglyLinkedList();
sll.head = new ListNode(10);
ListNode second = new ListNode(8);
ListNode third = new ListNode(1);
ListNode fourth = new ListNode(10);
sll.head.next = second;
second.next = third;
third.next = fourth;
sll.display();
}
}
【问题讨论】:
标签: java data-structures linked-list output singly-linked-list