Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 78 additions & 3 deletions DIRECTORY.md

Large diffs are not rendered by default.

40 changes: 40 additions & 0 deletions DeleteMiddleNode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.thealgorithms.linkedlist;

public class DeleteMiddleNode {

public static ListNode deleteMiddle(ListNode head) {
if (head == null || head.next == null) return null;

ListNode slow = head, fast = head, prev = null;
while (fast != null && fast.next != null) {
prev = slow;
slow = slow.next;
fast = fast.next.next;
}
prev.next = slow.next; // remove middle node
return head;
}

public static void printList(ListNode head) {
while (head != null) {
System.out.print(head.val + " -> ");
head = head.next;
}
System.out.println("null");
}

public static void main(String[] args) {
ListNode head = new ListNode(1);
head.next = new ListNode(2);
head.next.next = new ListNode(3);
head.next.next.next = new ListNode(4);
head.next.next.next.next = new ListNode(5);

System.out.print("Original list: ");
printList(head);

head = deleteMiddle(head);
System.out.print("After deleting middle: ");
printList(head);
}
}
32 changes: 32 additions & 0 deletions DetectCycleInLL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.thealgorithms.linkedlist;

class ListNode {
int val;
ListNode next;
ListNode(int val) { this.val = val; this.next = null; }
}

public class DetectCycle {

public static boolean hasCycle(ListNode head) {
ListNode slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) {
return true; // cycle detected
}
}
return false; // no cycle
}

public static void main(String[] args) {
ListNode head = new ListNode(3);
head.next = new ListNode(2);
head.next.next = new ListNode(0);
head.next.next.next = new ListNode(-4);
head.next.next.next.next = head.next; // create a cycle

System.out.println("Cycle exists: " + hasCycle(head));
}
}
24 changes: 24 additions & 0 deletions FindMiddleNode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.thealgorithms.linkedlist;

public class FindMiddleNode {

public static ListNode findMiddle(ListNode head) {
ListNode slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
return slow; // middle node
}

public static void main(String[] args) {
ListNode head = new ListNode(1);
head.next = new ListNode(2);
head.next.next = new ListNode(3);
head.next.next.next = new ListNode(4);
head.next.next.next.next = new ListNode(5);

ListNode middle = findMiddle(head);
System.out.println("Middle node value: " + middle.val);
}
}
Empty file added LinkedList.md
Empty file.
35 changes: 35 additions & 0 deletions Sort_0s_1s_2s.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.thealgorithms.linkedlist;

import java.util.Arrays;

public class Sort012 {

public static void sortColors(int[] nums) {
int low = 0, mid = 0, high = nums.length - 1;

while (mid <= high) {
switch (nums[mid]) {
case 0 -> {
int temp = nums[low];
nums[low] = nums[mid];
nums[mid] = temp;
low++;
mid++;
}
case 1 -> mid++;
case 2 -> {
int temp = nums[mid];
nums[mid] = nums[high];
nums[high] = temp;
high--;
}
}
}
}

public static void main(String[] args) {
int[] nums = {2, 0, 2, 1, 1, 0};
sortColors(nums);
System.out.println(Arrays.toString(nums));
}
}