File tree 3 files changed +59
-0
lines changed
3 files changed +59
-0
lines changed Original file line number Diff line number Diff line change
1
+ // java.util.* and java.util.streams.* have been imported for this problem.
2
+ // You don't need any other imports.
3
+
4
+ public ListNode deleteAtMiddle (ListNode head , int position ) {
5
+ if (head == null ) return head ;
6
+
7
+ // if head needs to be deleted
8
+ if (position == 1 ) {
9
+ return head .next ;
10
+ }
11
+
12
+ ListNode prev = head ;
13
+
14
+ // find prev node of the node to be deleted
15
+ for (int i =1 ;i <position -1 ;i ++) {
16
+ if (prev == null || prev .next == null ) return head ;
17
+ prev = prev .next ;
18
+ }
19
+
20
+ prev .next = prev .next .next ;
21
+
22
+ return head ;
23
+ }
Original file line number Diff line number Diff line change
1
+ // java.util.* and java.util.streams.* have been imported for this problem.
2
+ // You don't need any other imports.
3
+
4
+ public ListNode insertAtHead (ListNode head , int data ) {
5
+ ListNode node = new ListNode (data );
6
+
7
+ if (head == null ) return node ;
8
+
9
+ node .next = head ;
10
+ return node ;
11
+ }
Original file line number Diff line number Diff line change
1
+ // java.util.* and java.util.streams.* have been imported for this problem.
2
+ // You don't need any other imports.
3
+
4
+ public static boolean isIsomorphic (String input1 , String input2 ) {
5
+ if ((input1 == null || input2 == null )) return true ;
6
+ if (input1 .length () != input2 .length ()) return false ;
7
+
8
+ Hashtable <Character , Integer > table1 = new Hashtable <Character , Integer >();
9
+ Hashtable <Character , Integer > table2 = new Hashtable <Character , Integer >();
10
+
11
+ for (int i =0 ;i <input1 .length ();i ++) {
12
+ char c1 = input1 .charAt (i );
13
+ char c2 = input2 .charAt (i );
14
+
15
+ int count1 = table1 .getOrDefault (c1 , 0 ) + 1 ;
16
+ table1 .put (c1 , count1 );
17
+
18
+ int count2 = table2 .getOrDefault (c2 , 0 ) + 1 ;
19
+ table2 .put (c2 , count2 );
20
+
21
+ if (count1 != count2 ) return false ;
22
+ }
23
+
24
+ return true ;
25
+ }
You can’t perform that action at this time.
0 commit comments