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 f739f55

Browse files
Merge remote-tracking branch 'origin/master'
2 parents f5c5972 + efc0b39 commit f739f55

File tree

8 files changed

+520
-1
lines changed

8 files changed

+520
-1
lines changed

‎CONTRIBUTING.md

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

‎DoublyLinkedList/DoublyLinkedList.java

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,135 @@
11
package DoublyLinkedList;
22

33
public class DoublyLinkedList {
4+
Node head;
5+
Node tail;
6+
int size = 0;
7+
8+
// add the node at last
9+
public void add(int data) {
10+
Node node = new Node(data);
11+
if (head == null) {
12+
head = node;
13+
node.next = null;
14+
node.prev = head;
15+
} else {
16+
node.prev = tail;
17+
tail.next = node;
18+
}
19+
tail = node;
20+
size ++;
21+
}
22+
23+
// add node at a index
24+
public void add (int data, int index) {
25+
Node node = new Node(data);
26+
if (index == 0) {
27+
addFirst(data);
28+
}
29+
else if (index == size - 1) {
30+
add(data);
31+
}
32+
else if (index >= size) {
33+
throw new IndexOutOfBoundsException("Index does not exists");
34+
}
35+
else {
36+
Node crr = head;
37+
int i = 1;
38+
while (i < index - 1) {
39+
// System.out.println(crr.data);
40+
crr = crr.next;
41+
i++;
42+
}
43+
node.next = crr.next;
44+
node.prev = crr;
45+
crr.next = node;
46+
size++;
47+
}
48+
}
49+
// add the node at first
50+
public void addFirst(int data) {
51+
Node node = new Node(data);
52+
node.next = head;
53+
head = node;
54+
node.prev = head;
55+
size ++;
56+
}
57+
58+
// delete a node from last
59+
public int delete() {
60+
int data = 0;
61+
if (head == null)
62+
throw new IndexOutOfBoundsException("Linked List is Empty!!");
63+
else {
64+
data = tail.data;
65+
tail.prev.next = null;
66+
tail = tail.next;
67+
size --;
68+
}
69+
return data;
70+
}
71+
72+
// delete a node from start
73+
public int deleteFirst() {
74+
int data = 0;
75+
if (head == null)
76+
throw new ArrayIndexOutOfBoundsException("Linked List is Empty!!");
77+
else {
78+
data = head.data;
79+
head = head.next;
80+
head.prev = head;
81+
size --;
82+
}
83+
return data;
84+
}
85+
86+
// display the list
87+
public void display() {
88+
Node 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+
// delete a node at any index
97+
public int delete(int index) {
98+
int i = 0, data = 0;
99+
Node crr = head;
100+
while (i ++ < index - 1) {
101+
crr = crr.next;
102+
}
103+
data = crr.next.data;
104+
crr.next = crr.next.next;
105+
crr.next.prev = crr;
106+
size --;
107+
return data;
108+
}
109+
110+
public static void main(String[] args) {
111+
DoublyLinkedList dll = new DoublyLinkedList();
112+
dll.add(10);
113+
dll.add(20);
114+
dll.add(30);
115+
dll.display();
116+
117+
dll.addFirst(6);
118+
dll.display();
119+
120+
dll.addFirst(5);
121+
dll.display();
122+
123+
dll.add(25, 2);
124+
dll.display();
125+
126+
System.out.println("Last Deleted Node => " + dll.delete());
127+
dll.display();
128+
129+
System.out.println("First Deleted Node => " + dll.deleteFirst());
130+
dll.display();
131+
132+
System.out.println("Deleted Node => " + dll.delete(2));
133+
dll.display();
134+
}
4135
}

‎Leetcode/Leetcode_20.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.Stack;
4+
5+
public class Leetcode_20 {
6+
public boolean isValid(String s) {
7+
Stack<Integer> st =new Stack<Integer>();
8+
char a[]=s.toCharArray();
9+
int comp=0;
10+
for(int i=0;i<s.length();i++){
11+
if(i>0 && !st.isEmpty()){comp=st.peek();}
12+
st.push((int) a[i]);
13+
if(comp!=0 && !st.isEmpty() && st.size()>1){
14+
if((comp==91 && st.peek()==93)||(comp==123 && st.peek()==125)||(comp==40 && st.peek()==41)){
15+
st.pop();st.pop();}
16+
}
17+
}
18+
return st.isEmpty();
19+
}
20+
}

‎Leetcode/Leetcode_203.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package Leetcode;
2+
3+
import DSA_College.LinkList.ListNode;
4+
5+
import java.util.HashMap;
6+
7+
public class Leetcode_203 {
8+
public ListNode removeElements(ListNode head, int val) {
9+
if(head == null)
10+
return head;
11+
ListNode curr = head;
12+
ListNode pre = null;
13+
while(curr != null){
14+
if(curr.data == val && pre!=null){
15+
pre.next = curr.next;
16+
curr = curr.next;
17+
}else if(curr.data == val){
18+
curr = curr.next;
19+
head = curr;
20+
} else{
21+
pre = curr;
22+
curr = curr.next;
23+
}
24+
25+
}
26+
27+
return head;
28+
}
29+
}

‎Leetcode/Leetcode_225.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package Leetcode;
2+
3+
import java.util.LinkedList;
4+
import java.util.Queue;
5+
6+
public class Leetcode_225 {
7+
Queue<Integer> q1;
8+
Queue<Integer> q2;
9+
Leetcode_225() {
10+
this.q1 = new LinkedList<>();
11+
this.q2 = new LinkedList<>();
12+
}
13+
14+
public void push(int x) {
15+
q1.add(x);
16+
while (!q1.isEmpty())
17+
q2.add(q1.remove());
18+
while (!q2.isEmpty())
19+
q1.add(q2.remove());
20+
}
21+
22+
public int pop() {
23+
if (q1.isEmpty())
24+
return -1;
25+
while (!q1.isEmpty())
26+
q2.add(q1.remove());
27+
int res = q2.remove();
28+
while (!q2.isEmpty())
29+
q1.add(q2.remove());
30+
return res;
31+
}
32+
33+
public int top() {
34+
if (q1.isEmpty())
35+
return -1;
36+
while (!q1.isEmpty())
37+
q2.add(q1.remove());
38+
int res = q2.peek();
39+
while (!q2.isEmpty())
40+
q1.add(q2.remove());
41+
return res;
42+
}
43+
44+
public boolean empty() {
45+
return q1.isEmpty();
46+
}
47+
}

‎Leetcode/Leetcode_232.java

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package Leetcode;
2+
3+
import java.util.Stack;
4+
5+
public class Leetcode_232 {
6+
int peek;
7+
Stack<Integer> s1;
8+
Stack<Integer> s2;
9+
// public Leetcode_232() {
10+
// this.s1 = new Stack<>();
11+
// this.s2 = new Stack<>();
12+
// }
13+
//
14+
// public void push(int x) {
15+
// s1.push(x);
16+
// }
17+
//
18+
// public int pop() {
19+
// if (empty())
20+
// return -1;
21+
// while (!s1.isEmpty()) {
22+
// s2.push(s1.pop());
23+
// }
24+
// peek = s2.peek();
25+
// int data = s2.pop();
26+
// while (!s2.isEmpty())
27+
// s1.push(s2.pop());
28+
// return data;
29+
// }
30+
//
31+
// public int peek() {
32+
// if (s1.isEmpty())
33+
// return -1;
34+
// return peek;
35+
// }
36+
//
37+
// public boolean empty() {
38+
// return s1.isEmpty();
39+
// }
40+
Leetcode_232() {
41+
s1 = new Stack<>();
42+
s2 = new Stack<>();
43+
}
44+
45+
public void push(int x) {
46+
if(empty()) {
47+
s1.push(x);
48+
}else {
49+
while(s1.size()>0) {
50+
s2.push(s1.pop());
51+
}
52+
s1.push(x);
53+
while(s2.size()>0) {
54+
s1.push(s2.pop());
55+
}
56+
}
57+
58+
}
59+
60+
public int pop() {
61+
if(empty())
62+
return -1;
63+
return s1.pop();
64+
}
65+
66+
public int peek() {
67+
if(empty())
68+
return -1;
69+
return s1.peek();
70+
}
71+
72+
public boolean empty() {
73+
return s1.isEmpty();
74+
}
75+
76+
public static void main(String[] args) {
77+
Leetcode_232 l = new Leetcode_232();
78+
l.push(10);
79+
l.push(20);
80+
l.push(30);
81+
l.push(40);
82+
System.out.println(l.empty());
83+
System.out.println(l.peek());
84+
System.out.println(l.pop());
85+
}
86+
}

‎MainClass.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import java.util.Comparator;
22
import java.util.LinkedList;
33
import java.util.Scanner;
4-
4+
// this is the main class.
55
public class MainClass {
66
public static void main(String[] args) {
77
LinkedList <Friend> friends = new LinkedList<>();

0 commit comments

Comments
(0)

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