-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkEg.java
More file actions
48 lines (41 loc) · 1.26 KB
/
LinkEg.java
File metadata and controls
48 lines (41 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import java.util.LinkedList;
import java.util.Scanner;
public class LinkEg
{
public static void main(String[] args)
{
// LinkedList li = new LinkedList();
// li.addFirst(1);
// System.out.println(li.getFirst());
// ListNode one = new ListNode(1);
// ListNode two = new ListNode(2,one);
Scanner scanner = new Scanner(System.in);
ListNode head = null;
ListNode current = null;
System.out.println("请输入整数(输入-1结束输入):");
int num = scanner.nextInt();
while (num != -1)
{
ListNode newNode = new ListNode(num);
if (head == null)
{
head = newNode;
current = newNode;
} else
{
current.next = newNode;
current = newNode;
}
num = scanner.nextInt();
}
scanner.close();
//ListNode current = head; 假设 head 是链表的头节点
current = head;
while (current != null)
{
System.out.print(current.val + " "); // 打印当前节点的值
current = current.next; // 移动到下一个节点
}
System.out.println(); // 换行
}
}