Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9f7e002

Browse files
2 parents a47d639 + b169cda commit 9f7e002

16 files changed

+503
-0
lines changed
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
package DSA_College.DoublyLinkedList;
2+
3+
public class DoublyLinkedList {
4+
ListNode head;
5+
ListNode tail;
6+
int size = 0;
7+
8+
DoublyLinkedList() {
9+
this.head = null;
10+
this.tail = null;
11+
this.size = 0;
12+
}
13+
14+
// ListNode class
15+
private static class ListNode {
16+
int data;
17+
ListNode prev;
18+
ListNode next;
19+
20+
ListNode(int data) {
21+
this.data = data;
22+
this.prev = null;
23+
this.next = null;
24+
}
25+
}
26+
27+
// add node at start
28+
public void insertFirst(int data) {
29+
ListNode node = new ListNode(data);
30+
if (head == null) {
31+
head = node;
32+
node.next = null;
33+
tail = node;
34+
} else {
35+
node.next = head;
36+
node.prev = null;
37+
head = node;
38+
}
39+
size ++;
40+
}
41+
42+
// insert at index
43+
public void insert(int index, int data) {
44+
ListNode node = new ListNode(data);
45+
if (head == null) {
46+
insertFirst(data);
47+
} else {
48+
ListNode n = head;
49+
int i = 0;
50+
while (i < index - 1) {
51+
n = n.next;
52+
i ++;
53+
}
54+
node.next = n.next;
55+
n.next.prev = node;
56+
n.next = node;
57+
node.prev = n;
58+
}
59+
size ++;
60+
}
61+
62+
// insert at last
63+
public void insertLast(int data) {
64+
ListNode node = new ListNode(data);
65+
if (head == null) {
66+
insertFirst(data);
67+
} else {
68+
node.prev = tail;
69+
tail.next = node;
70+
node.next = null;
71+
tail = node;
72+
size++;
73+
}
74+
}
75+
76+
// returns size of the linked list
77+
public int size() {
78+
return size;
79+
}
80+
81+
// method to check is linked list empty or not
82+
public boolean isEmpty() {
83+
return size == 0;
84+
}
85+
86+
// display forward
87+
public void displayForward() {
88+
ListNode n = head;
89+
while (n != null) {
90+
System.out.print(n.data + " -> ");
91+
n = n.next;
92+
}
93+
System.out.println("null");
94+
}
95+
96+
// display backward
97+
public void displayBackward() {
98+
ListNode n = tail;
99+
while (n != null) {
100+
System.out.print(n.data + " -> ");
101+
n = n.prev;
102+
}
103+
System.out.println("null");
104+
}
105+
106+
// delete at first
107+
public int deleteFirst() {
108+
if (isEmpty()) {
109+
System.out.println("List is Empty");
110+
return -1;
111+
}
112+
if (head == tail) {
113+
int data = head.data;
114+
head = tail = null;
115+
return data;
116+
}
117+
int data = head.data;
118+
head = head.next;
119+
head.prev = null;
120+
return data;
121+
}
122+
123+
// delete last
124+
public int deleteLast() {
125+
if (isEmpty()) {
126+
System.out.println("List is Empty");
127+
return -1;
128+
}
129+
if (head == tail) {
130+
int data = head.data;
131+
head = tail = null;
132+
return data;
133+
}
134+
int data = tail.data;
135+
tail = tail.next;
136+
tail.prev = null;
137+
return data;
138+
}
139+
140+
141+
public static void main(String[] args) {
142+
DoublyLinkedList dll = new DoublyLinkedList();
143+
dll.insertFirst(10);
144+
dll.insertFirst(20);
145+
dll.insertFirst(30);
146+
dll.displayForward();
147+
System.out.println(dll.size());
148+
149+
dll.insertLast(5);
150+
dll.displayForward();
151+
System.out.println(dll.size());
152+
153+
dll.insert(2, 25);
154+
dll.displayForward();
155+
System.out.println(dll.size());
156+
157+
dll.displayBackward();
158+
159+
System.out.println();
160+
dll.displayForward();
161+
System.out.println("Deleted Node => " + dll.deleteFirst());
162+
dll.displayForward();
163+
System.out.println(dll.size());
164+
}
165+
}

‎Leetcode/Leetcode_101.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

‎Leetcode/Leetcode_1071.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package Leetcode;
2+
3+
public class Leetcode_1071 {
4+
public static String gcdOfStrings(String str1, String str2) {
5+
String r = "";
6+
int len = Math.min(str1.length(), str2.length());
7+
for (int i = 0; i < len; i++) {
8+
if (true)
9+
return "";
10+
}
11+
return r;
12+
}
13+
14+
public static void main(String[] args) {
15+
System.out.println(gcdOfStrings("ABCABC", "ABC"));
16+
}
17+
}

‎Leetcode/Leetcode_112.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package Leetcode;
2+
3+
4+
//class Leetcode_112 {
5+
// public boolean hasPathSum(TreeNode root, int targetSum) {
6+
// if (root == null)
7+
// return false;
8+
// if (root.left == null && root.right == null)
9+
// return root.val - targetSum == 0;
10+
// return hasPathSum(root.left, targetSum - root.val) || hasPathSum(root.right, targetSum - root.val);
11+
// }
12+
//}

‎Leetcode/Leetcode_155.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package Leetcode;
2+
3+
import java.util.Stack;
4+
// Leetcode 155
5+
class MinStack {
6+
Stack<Integer> s;
7+
public MinStack() {
8+
s = new Stack<>();
9+
}
10+
11+
public void push(int val) {
12+
s.push(val);
13+
}
14+
15+
public void pop() {
16+
s.pop();
17+
}
18+
19+
public int top() {
20+
return s.peek();
21+
}
22+
23+
public int getMin() {
24+
Stack<Integer> s2 = new Stack<>();
25+
int min = Integer.MAX_VALUE;
26+
while (!s.isEmpty()) {
27+
int a = s.pop();
28+
min = Integer.min(a, min);
29+
s2.push(a);
30+
}
31+
while (!s2.isEmpty()) {
32+
int a = s2.pop();
33+
s.push(a);
34+
}
35+
36+
return min;
37+
}
38+
}

‎Leetcode/Leetcode_169.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package Leetcode;
2+
3+
import java.util.HashMap;
4+
// majority element
5+
public class Leetcode_169 {
6+
public static int majorityElement(int[] nums) {
7+
HashMap<Integer, Integer> h = new HashMap<>();
8+
for (int num : nums)
9+
h.put(num, h.getOrDefault(num, 0) + 1);
10+
11+
for (int num : nums)
12+
if (h.get(num) > (nums.length / 2))
13+
return num;
14+
return -1;
15+
}
16+
17+
public static void main(String[] args) {
18+
System.out.println(majorityElement(new int[]{2,2,1,1,1,2,2}));
19+
}
20+
}

‎Leetcode/Leetcode_226.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package Leetcode;
2+
3+
class TreeNode {
4+
int val;
5+
TreeNode left;
6+
TreeNode right;
7+
8+
TreeNode() {
9+
}
10+
11+
TreeNode(int val) {
12+
this.val = val;
13+
}
14+
15+
TreeNode(int val, TreeNode left, TreeNode right) {
16+
this.val = val;
17+
this.left = left;
18+
this.right = right;
19+
}
20+
}
21+
22+
23+
public class Leetcode_226 {
24+
public TreeNode invertTree(TreeNode root) {
25+
if (root == null)
26+
return null;
27+
TreeNode left = invertTree(root.left);
28+
TreeNode right = invertTree(root.right);
29+
30+
// swapping of nodes
31+
root.right = left;
32+
root.left = right;
33+
34+
return root;
35+
}
36+
}

‎Leetcode/Leetcode_231.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package Leetcode;
2+
3+
public class Leetcode_231 {
4+
// iterative approach
5+
// public boolean isPowerOfTwo(int n) {
6+
// if (n <= 0)
7+
// return false;
8+
// while(n > 1) {
9+
// if (n % 2 != 0)
10+
// return false;
11+
// n = n / 2;
12+
// }
13+
// return true;
14+
// }
15+
16+
// bitwise approach
17+
// public static boolean isPowerOfTwo(int n) {
18+
// int a = Integer.bitCount(n);
19+
// return a == 1;
20+
//
21+
// }
22+
23+
// bitwise approach 2
24+
public static boolean isPowerOfTwo(int n) {
25+
if (n == 0)
26+
return false;
27+
return ((long) n & ((long) n - 1)) == 0;
28+
}
29+
30+
public static void main(String[] args) {
31+
System.out.println(isPowerOfTwo(28));
32+
}
33+
}

‎Leetcode/Leetcode_240.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package Leetcode;
2+
3+
import java.util.Arrays;
4+
5+
public class Leetcode_240 {
6+
public static boolean searchMatrix(int[][] matrix, int target) {
7+
return searchRow(matrix, target);
8+
}
9+
public static boolean searchRow(int[][] m, int target){
10+
for (int[] ints : m) {
11+
boolean r = binarySearch(ints, target);
12+
if (r == true)
13+
return true;
14+
}
15+
return false;
16+
}
17+
18+
public static boolean binarySearch(int[] m, int target){
19+
int r = Arrays.binarySearch(m, target);
20+
return r >= 0;
21+
}
22+
23+
public static void main(String[] args) {
24+
int target = 5;
25+
int[][] m = {{1,4,7,11,15},{2,5,8,12,19},{3,6,9,16,22},{10,13,14,17,24},{18,21,23,26,30}};
26+
System.out.println(searchMatrix(m, target));
27+
}
28+
}

‎Leetcode/Leetcode_26.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package Leetcode;
2+
// remove duplicates from a sorted array
3+
4+
public class Leetcode_26 {
5+
public static int removeDuplicates(int[] nums) {
6+
StringBuilder a = new StringBuilder();
7+
for (int num : nums) {
8+
if (!a.toString().contains(num + "")) {
9+
a.append(num);
10+
}
11+
}
12+
for (int i = 0; i < a.length(); i++) {
13+
nums[i] = Integer.parseInt(a.charAt(i) + "");
14+
}
15+
return a.length();
16+
}
17+
18+
public static void main(String[] args) {
19+
System.out.println(removeDuplicates(new int[]{0,0,1,1,1,2,2,3,3,4}));
20+
}
21+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /