主要功能
- 基本表示
- 链表的创建
- 打印树的结构
- 打印
后续会根据功能代码详细划分
总体代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| public class ListNode {
public int val; public ListNode next;
public ListNode() { }
public ListNode(int val) { this.val = val; }
public ListNode(int val, ListNode next) { this.val = val; this.next = next; }
public static ListNode createList(String date) { return createList(toIntArray(date)); }
public static ListNode createList(int[] num) { ListNode node = new ListNode(), head = node; for (int i : num) { node.next = new ListNode(i); node = node.next; } return head.next; } public void addLast(ListNode newNode) { ListNode node = this; while (node.next != null) node = node.next; node.next = newNode; }
public void addLast(String date) { addLast(toIntArray(date)); }
public void addLast(int[] num) { addLast(createList(num)); } private static int[] toIntArray(String data) { String[] S = data.replace("[", "").replace("]", "").split(","); int len = S.length; int[] ans = new int[len]; for (int i = 0; i < len; i++) { ans[i] = Integer.parseInt(S[i]); } return ans; }
public static void show(ListNode node) { while (node != null) { System.out.format("%d ", node.val); node = node.next; } System.out.println(); }
@Override public String toString() { StringBuilder sb = new StringBuilder(); ListNode node = this; while (node != null) { sb.append(node.val).append(" "); node = node.next; } return sb.toString(); } }
|
版权声明: 此文章版权归BrownCutie所有,如有转载,请註明来自原作者