您好,欢迎来到二三娱乐。
搜索
您的当前位置:首页OJ程序积木 Java

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;
}

Copyright © 2019- yule263.com 版权所有 湘ICP备2023023988号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务