Skip to content

Commit 86c1189

Browse files
committed
Added java dsa code by Ankan Sen
1 parent ff9fd2e commit 86c1189

File tree

5 files changed

+131
-0
lines changed

5 files changed

+131
-0
lines changed

DeleteMiddleNode.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.thealgorithms.linkedlist;
2+
3+
public class DeleteMiddleNode {
4+
5+
public static ListNode deleteMiddle(ListNode head) {
6+
if (head == null || head.next == null) return null;
7+
8+
ListNode slow = head, fast = head, prev = null;
9+
while (fast != null && fast.next != null) {
10+
prev = slow;
11+
slow = slow.next;
12+
fast = fast.next.next;
13+
}
14+
prev.next = slow.next; // remove middle node
15+
return head;
16+
}
17+
18+
public static void printList(ListNode head) {
19+
while (head != null) {
20+
System.out.print(head.val + " -> ");
21+
head = head.next;
22+
}
23+
System.out.println("null");
24+
}
25+
26+
public static void main(String[] args) {
27+
ListNode head = new ListNode(1);
28+
head.next = new ListNode(2);
29+
head.next.next = new ListNode(3);
30+
head.next.next.next = new ListNode(4);
31+
head.next.next.next.next = new ListNode(5);
32+
33+
System.out.print("Original list: ");
34+
printList(head);
35+
36+
head = deleteMiddle(head);
37+
System.out.print("After deleting middle: ");
38+
printList(head);
39+
}
40+
}

DetectCycleInLL.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.thealgorithms.linkedlist;
2+
3+
class ListNode {
4+
int val;
5+
ListNode next;
6+
ListNode(int val) { this.val = val; this.next = null; }
7+
}
8+
9+
public class DetectCycle {
10+
11+
public static boolean hasCycle(ListNode head) {
12+
ListNode slow = head, fast = head;
13+
while (fast != null && fast.next != null) {
14+
slow = slow.next;
15+
fast = fast.next.next;
16+
if (slow == fast) {
17+
return true; // cycle detected
18+
}
19+
}
20+
return false; // no cycle
21+
}
22+
23+
public static void main(String[] args) {
24+
ListNode head = new ListNode(3);
25+
head.next = new ListNode(2);
26+
head.next.next = new ListNode(0);
27+
head.next.next.next = new ListNode(-4);
28+
head.next.next.next.next = head.next; // create a cycle
29+
30+
System.out.println("Cycle exists: " + hasCycle(head));
31+
}
32+
}

FindMiddleNode.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.thealgorithms.linkedlist;
2+
3+
public class FindMiddleNode {
4+
5+
public static ListNode findMiddle(ListNode head) {
6+
ListNode slow = head, fast = head;
7+
while (fast != null && fast.next != null) {
8+
slow = slow.next;
9+
fast = fast.next.next;
10+
}
11+
return slow; // middle node
12+
}
13+
14+
public static void main(String[] args) {
15+
ListNode head = new ListNode(1);
16+
head.next = new ListNode(2);
17+
head.next.next = new ListNode(3);
18+
head.next.next.next = new ListNode(4);
19+
head.next.next.next.next = new ListNode(5);
20+
21+
ListNode middle = findMiddle(head);
22+
System.out.println("Middle node value: " + middle.val);
23+
}
24+
}

LinkedList.md

Whitespace-only changes.

Sort_0s_1s_2s.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.thealgorithms.linkedlist;
2+
3+
import java.util.Arrays;
4+
5+
public class Sort012 {
6+
7+
public static void sortColors(int[] nums) {
8+
int low = 0, mid = 0, high = nums.length - 1;
9+
10+
while (mid <= high) {
11+
switch (nums[mid]) {
12+
case 0 -> {
13+
int temp = nums[low];
14+
nums[low] = nums[mid];
15+
nums[mid] = temp;
16+
low++;
17+
mid++;
18+
}
19+
case 1 -> mid++;
20+
case 2 -> {
21+
int temp = nums[mid];
22+
nums[mid] = nums[high];
23+
nums[high] = temp;
24+
high--;
25+
}
26+
}
27+
}
28+
}
29+
30+
public static void main(String[] args) {
31+
int[] nums = {2, 0, 2, 1, 1, 0};
32+
sortColors(nums);
33+
System.out.println(Arrays.toString(nums));
34+
}
35+
}

0 commit comments

Comments
 (0)