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 b169cda

Browse files
added leetcode questions
1 parent 958f350 commit b169cda

File tree

3 files changed

+213
-0
lines changed

3 files changed

+213
-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_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_392.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package Leetcode;
2+
3+
//public class Leetcode_392 {
4+
// public boolean isSubsequence(String s, String t) {
5+
// if (s.equals(t) || s.isEmpty())
6+
// return true;
7+
// if (t.length() < s.length())
8+
// return false;
9+
// int j, i;
10+
// i = j = 0;
11+
// for (int i1 = 0; i1 < t.length(); i1++) {
12+
//
13+
// }
14+
// }
15+
//}

0 commit comments

Comments
(0)

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