Skip to content

Commit 7540242

Browse files
committed
Added LL programs :octocat:
1 parent c6fff8f commit 7540242

30 files changed

+40
-128
lines changed

Categories/Linked List/Cycle.java renamed to Categories/LinkedList/LinkedListCycle.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class ListNode {
44
ListNode(int x) { val = x; }
55
}
66

7-
public class Cycle {
7+
public class LinkedListCycle {
88
public boolean hasCycle(ListNode head) {
99
ListNode slow = head;
1010
ListNode fast = head;

Categories/LinkedList/Test.class

405 Bytes
Binary file not shown.
File renamed without changes.

Categories/Median/MedianOfSortedArray.java

-49
This file was deleted.

Categories/String/WordPattern.java

-42
This file was deleted.
File renamed without changes.
File renamed without changes.

Easy/MergeTwoSortedLists.java

-27
This file was deleted.

Easy/ReverseInteger.java

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int reverse(int x) {
3+
boolean overflowed = false;
4+
int result = 0;
5+
6+
while( x != 0){
7+
int num = x % 10;
8+
x = x / 10;
9+
if((result * 10)/100 != result/10)//MEans overflowed
10+
overflowed = true;
11+
12+
result = (result*10) + num;
13+
}
14+
return overflowed ? 0 : result;
15+
}
16+
}
File renamed without changes.

Easy/WordPattern.java

+23-9
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,42 @@
1+
package String;
2+
13
import java.util.*;
4+
public class WordPattern{
5+
public static void main(String[] args) {
6+
String pattern = "abba";
7+
String str = "dog dog dog dog";
8+
if(isWordPattern(pattern,str))
9+
System.out.println("TRUE");
10+
else
11+
System.out.println("FALSE");
12+
}
213

3-
class Solution {
4-
public boolean wordPattern(String pattern, String str) {
14+
public static boolean isWordPattern(String pattern, String str){
15+
16+
String[] arr = str.split(" ");
517

6-
String[] arr = str.split(" ");
7-
818
//prevent out of boundary problem
919
if(arr.length != pattern.length())
1020
return false;
1121

1222
HashMap<Character, String> map = new HashMap<Character, String>();
1323
for(int i=0; i<pattern.length(); i++){
1424
char c = pattern.charAt(i);
15-
if(map.containsKey(c)){
25+
26+
if(map.containsKey(c)){ //if already present
27+
1628
String value = map.get(c);
17-
if(!value.equals(arr[i])){
29+
30+
if(!value.equals(arr[i])) //if value doesn't mtches with str eg b=dog
1831
return false;
19-
}
32+
2033
}else if (map.containsValue(arr[i])){
2134
return false;
2235
}
36+
2337
map.put(c, arr[i]);
2438
}
2539

26-
return true;
27-
}
40+
return true;
41+
}
2842
}

0 commit comments

Comments
 (0)