public class LinkedList {
class ListNode {
int val;
ListNode next;
ListNode() {
}
ListNode(int val) {
this.val = val;
}
}
public ListNode head;
public ListNode tail;
public static void main(String[] args) {
int[] nums = new int[]{4,6,7,8,3,6,9};
LinkedList list = new LinkedList();
for (int num :nums) list.add(num);
list.print(list.head);
}
/**
* 链表添加数据
* @param date
*/
public void add(int date) {
if (head == null) {
head = new ListNode(date);
tail = head;
} else {
tail.next = new ListNode(date);
tail = tail.next;
}
}
/**
* 打印链表
* @param node
*/
public void print(ListNode node){
if(node == null) return;
tail = node;
while(tail != null){
System.out.print(tail.val + " ");
tail = tail.next;
}
}
}
链表(评测机)
最新推荐文章于 2024-06-12 13:39:23 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)