forked from Wang-Jun-Chao/Cracking-the-Coding-Interview
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.java
79 lines (63 loc) · 1.85 KB
/
Main.java
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
77
78
79
/**
* Author: 王俊超
* Date: 2016-01-07 09:47
* CSDN: http://blog.csdn.net/derrantcm
* Github: https://github.com/Wang-Jun-Chao
* Declaration: All Rights Reserved !!!
*/
public class Main {
public static void main(String[] args) {
ListNode head = makeList();
Solution s = new Solution();
for (int i = 1; i <= 10; i++) {
ListNode n = s.FindKthToTail(head, i);
System.out.println(n == null ? null : n.val);
}
}
public static ListNode makeList() {
ListNode head = new ListNode(1);
ListNode next = head;
for (int i = 2; i <= 9; i++) {
next.next = new ListNode(i);
next = next.next;
}
return head;
}
/**
* 6
* / \
* 5 9
* / \ / \
* 3 4 8 10
* / \ / \
* 1 2 7 11
* @return
*/
public static TreeNode makeTree() {
TreeNode root = new TreeNode(6);
root.left = new TreeNode(5);
root.right = new TreeNode(9);
root.left.left = new TreeNode(3);
root.left.right = new TreeNode(4);
root.left.left.left = new TreeNode(1);
root.left.left.right = new TreeNode(2);
root.right.left = new TreeNode(8);
root.right.left.left = new TreeNode(7);
root.right.right = new TreeNode(10);
root.right.right.right = new TreeNode(11);
return root;
}
public static void printTree(TreeNode root) {
if (root != null) {
printTree(root.left);
System.out.print(root.val + "->");
printTree(root.right);
}
}
public static void printList(ListNode head) {
while (head != null) {
System.out.print(head.val + "->");
head = head.next;
}
}
}