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