File tree 2 files changed +56
-0
lines changed
2 files changed +56
-0
lines changed Original file line number Diff line number Diff line change
1
+ package leetcode ;
2
+
3
+ public class LinkedListCycle {
4
+
5
+ public static void main (String [] agrs ){
6
+ ListNode r = new ListNode (3 );
7
+ r .next = new ListNode (2 );
8
+ r .next .next = new ListNode (0 );
9
+ r .next .next .next = new ListNode (-4 );
10
+ LinkedListCycle test = new LinkedListCycle ();
11
+ System .out .println (test .hasCycle (r ));
12
+ }
13
+
14
+ public boolean hasCycle (ListNode head ) {
15
+ if (head ==null || head .next ==null ) return false ;
16
+ ListNode l = head .next ;
17
+ while (l !=null ){
18
+ if (l ==head )
19
+ return true ;
20
+ l = l .next ;
21
+ }
22
+ return false ;
23
+ }
24
+ }
Original file line number Diff line number Diff line change
1
+ package leetcode ;
2
+
3
+ public class MinimumAbsoluteDifferenceBST {
4
+ private int prev =-1 ;
5
+ private int min = Integer .MAX_VALUE ;
6
+ public static void main (String [] args ){
7
+ TreeNode1 t = new TreeNode1 (1 );
8
+ t .right = new TreeNode1 (3 );
9
+ t .right .left = new TreeNode1 (2 );
10
+ MinimumAbsoluteDifferenceBST test = new MinimumAbsoluteDifferenceBST ();
11
+ System .out .println (test .getMinimumDifference (t ));
12
+ }
13
+
14
+ public int getMinimumDifference (TreeNode1 root ) {
15
+ if (root ==null ) return min ;
16
+ getMinimumDifference (root .left );
17
+ if (prev !=-1 )
18
+ min = Math .min (min , Math .abs (root .val -prev ));
19
+ prev =root .val ;
20
+ getMinimumDifference (root .right );
21
+ return min ;
22
+ }
23
+ }
24
+
25
+ class TreeNode1 {
26
+ int val ;
27
+ TreeNode1 left ;
28
+ TreeNode1 right ;
29
+ TreeNode1 (int x ){
30
+ val = x ;
31
+ }
32
+ }
You can’t perform that action at this time.
0 commit comments