结点类
public class Node{//数据域public Integer data;//指针域public Node next;public Node(){}public Node(int data){this.data = data;}public Node(int data, Node next){this.data = data;this.next = next;}}
链表实现
import java.util.Scanner;public class linkedList{static int N;public static void main(String[] args){Scanner sc = new Scanner(System.in);N = sc.nextInt();//头结点Node head = new Node();//临时结点,用于指向链表当前最后一个结点Node temp = head;for(int i=0; i<N; i++){Node p = new Node(sc.nextInt());if(head.next == null)head.next = p; //如果这是第一个创建的结点,则将头指针指向这个结点elsetemp.next = p; //如果不是第一个创建的结点,则将上一个结点的后继指针指向当前结点temp = p; //指针temp也指向当前结点}sc.close();//输出链表中所有数temp = head;while(temp.next != null){temp = temp.next;System.out.print(temp.data + " ");}}}
Input & Output
[Input Example]
92 3 5 8 9 10 18 26 32
[Output Example]
2 3 5 8 9 10 18 26 32
