public class ListNode { int val; ListNode next; ListNode() { } ListNode(int val) { this.val = val; } ListNode(int val, ListNode next) { this.val = val; this.next = next; } public static ListNode fromArray(int... values) { ListNode dumpHead = new ListNode(); ListNode head = dumpHead; for (int value : values) { head.next = new ListNode(value); head = head.next; } return dumpHead.next; } @Override public String toString() { StringBuilder sb = new StringBuilder(); ListNode tmpNode = this; sb.append(val); while (tmpNode.next != null) { sb.append(String.format(" , %s", tmpNode.next.val)); tmpNode = tmpNode.next; } return sb.toString(); } }