OJ程序积木 Java

1、单链表的结点:

public static class ListNode {
    int value;
    ListNode next;
}

2、栈

Stack<> stack = new Stack<>();
stack.push();
stack.pop();

3、二叉树的结点

public static class BinaryTreeNode {
    int value;
    BinaryTreeNode left;
    BinaryTreeNode right;
}

4、主动抛出异常

throw new RuntimeException("Xxx");

5、打印单链表

public static void printList(ListNode head) {
    while(head != null) {
        System.out.print(head.value + "->");
        head = head.next;
    }
    System.out.println("null");
}

6、打印数组元素

public static void printArray(int[] arr) {
    if(arr == null || arr.length <= 0) {
        return;
    }
    for (int i : arr) {
        System.out.print(i + " ");
    } 
    System.out.println();
}

7、先序遍历二叉树

public static void printTree(BinaryTreeNode node) {
    if(node != null) {
        System.out.print(node.value + " ");
        printTree(node.left);
        printTree(node.right);
    }
}

中序遍历二叉树

public static void printTree(BinaryTreeNode node) {
    if(node != null) {
        printTree(node.left);
        System.out.print(node.value + " ");
        printTree(node.right);
    }
}

后序遍历二叉树

public static void printTree(BinaryTreeNode node) {
    if(node != null) {
        printTree(node.left);
        printTree(node.right);
        System.out.print(node.value + " ");
    }
}

8、判断2个链表是否是同一个链表

public static boolean isSameList(ComplexListNode head1, ComplexListNode head2) {
    while (head1 != null && head2 != null) {
        if (head1 == head2) {
            head1 = head1.next;
            head2 = head2.next;
        } else {
            return false;
        }
    }
    return head1 == null && head2 == null;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容