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 0f5d221

Browse files
Add implementations for cycle detection in linked lists, stack reversal, and pushing elements to the bottom of a stack
1 parent 23f114b commit 0f5d221

File tree

4 files changed

+156
-0
lines changed

4 files changed

+156
-0
lines changed

‎LinkedList/CycleLinkedList.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
class ListNode {
2+
int val;
3+
ListNode next;
4+
ListNode(int x) {
5+
val = x;
6+
next = null;
7+
}
8+
}
9+
10+
public class CycleLinkedList {
11+
public boolean hasCycle(ListNode head) {
12+
13+
if(head == null){
14+
return false;
15+
}
16+
ListNode fast = head;
17+
ListNode slow = head;
18+
19+
while(fast != null && fast.next !=null){
20+
fast = fast.next.next;
21+
slow = slow.next;
22+
23+
if(fast == slow){
24+
return true;
25+
}
26+
}
27+
return false; // Added missing semicolon
28+
}
29+
30+
// Optional: Main method for testing
31+
public static void main(String[] args) {
32+
CycleLinkedList solution = new CycleLinkedList();
33+
34+
// Test case 1: No cycle
35+
ListNode head1 = new ListNode(1);
36+
head1.next = new ListNode(2);
37+
head1.next.next = new ListNode(3);
38+
head1.next.next.next = new ListNode(4);
39+
40+
System.out.println("Has cycle: " + solution.hasCycle(head1)); // false
41+
42+
// Test case 2: With cycle
43+
ListNode head2 = new ListNode(3);
44+
head2.next = new ListNode(2);
45+
head2.next.next = new ListNode(0);
46+
head2.next.next.next = new ListNode(-4);
47+
head2.next.next.next.next = head2.next; // Creates cycle
48+
49+
System.out.println("Has cycle: " + solution.hasCycle(head2)); // true
50+
}
51+
}

‎Stack/ExampleOne.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import java.util.Stack;
2+
3+
public class ExampleOne {
4+
public static void main(String[] args) {
5+
Stack<Integer> list = new Stack<>();
6+
7+
//push operation
8+
9+
list.push(10);
10+
list.push(20);
11+
list.push(30);
12+
list.push(40);
13+
list.push(50);
14+
15+
System.out.println(list);
16+
System.out.println(list.peek());
17+
18+
//pop operation
19+
20+
list.pop();
21+
list.pop();
22+
23+
System.out.println("After Pop operations " + list);
24+
25+
26+
boolean isEmpty = list.isEmpty();
27+
if(isEmpty){
28+
System.out.println("Yes stack is Empty ");
29+
}else{
30+
System.out.println("No , Stack is not Empty ");
31+
}
32+
}
33+
}

‎Stack/PushAtButtomProblem.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import java.util.Stack;
2+
3+
public class PushAtButtomProblem {
4+
public static void main(String[] args) {
5+
Stack<Integer> list = new Stack<>();
6+
list.push(10);
7+
list.push(20);
8+
list.push(30);
9+
list.push(40);
10+
11+
System.out.println("Atfirst the stack is : " +list);
12+
13+
PushAtButtom(list, 100);
14+
15+
System.out.println("After add the Element at the Buttom: " + list);
16+
17+
}
18+
19+
static void PushAtButtom(Stack<Integer> list,int element){
20+
//base case
21+
if(list.isEmpty()){
22+
list.push(element);
23+
return;
24+
}
25+
26+
int top = list.pop();
27+
PushAtButtom(list, element);
28+
list.push(top);
29+
}
30+
}

‎Stack/ReverseStack.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import java.util.Stack;
2+
3+
public class ReverseStack {
4+
public static void main(String[] args) {
5+
Stack<Integer> list = new Stack<>();
6+
list.push(10);
7+
list.push(20);
8+
list.push(30);
9+
list.push(40);
10+
11+
System.out.println("Before Reverse : " +list);
12+
13+
reverse(list);
14+
System.out.println("After Reverse: " +list);
15+
16+
17+
18+
}
19+
20+
static void PushAtButtom(Stack<Integer> list, int element) {
21+
// base case
22+
if (list.isEmpty()) {
23+
list.push(element);
24+
return;
25+
}
26+
27+
int top = list.pop();
28+
PushAtButtom(list, element);
29+
list.push(top);
30+
}
31+
32+
static void reverse(Stack<Integer> list){
33+
if(list.isEmpty()){
34+
return;
35+
}
36+
37+
int top = list.pop();
38+
reverse(list);
39+
PushAtButtom(list, top);
40+
41+
}
42+
}

0 commit comments

Comments
(0)

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