赞
踩
//尾插法 import java.util.Scanner; class ListNode { int val; ListNode next; public ListNode() {} public ListNode(int val) { this.val = val; } public ListNode(int val,ListNode next) { this.val = val; this.next = next; } } public class create { public static void main(String[] args) { Scanner sc = new Scanner(System.in); ListNode head = new ListNode(); int x =sc.nextInt(); while (x!=-1) { ListNode s = new ListNode(x); s.next = head.next; head.next = s; x = sc.nextInt(); } ListNode p = head.next; while(p!=null) { System.out.println(p.val); p = p.next; } } }
package Lianbiao; import java.util.Scanner; //头插法 public class create2 { public static void main(String[] args) { ListNode head = new ListNode(); ListNode p = head; Scanner sc = new Scanner(System.in); int x = sc.nextInt(); while(x!=-1) { ListNode s = new ListNode(x); p.next = s; p = s; x = sc.nextInt(); } p.next = null; p = head.next; while(p!=null) { System.out.println(p.val); p = p.next; } } } var foo = 'bar';
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。