Skip to content

Commit c4151e6

Browse files
add 3 problems
1 parent 7dfd2f8 commit c4151e6

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

deleteAtMiddle.java

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
}

insertAtHead.java

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
}

isIsomorphic.java

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
}

0 commit comments

Comments
 (0)