publicclassListNode{ publicint val; public ListNode next; publicListNode(int x){ val = x; }
@Override public String toString(){ String s = ""; ListNode current = this; while ( current != null ) { s = s + " " + current.val; current = current.next; } return s; } }
ListNodeUtil 类
编写ListNode初始化方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
publicclassListNodeUtil{ publicstatic ListNode initList(int...vals){ ListNode head = new ListNode(0); ListNode current = head; for(int val : vals){ current.next = new ListNode(val); current = current.next; } return head.next; }
@Test publicvoidtest(){ ListNode l = initList(1,2,3); System.out.println(l); } }