From aecdda9e25c91bed6717f7ea84572de7472add0e Mon Sep 17 00:00:00 2001 From: jeetusingh247 Date: Wed, 11 Feb 2026 17:18:57 +0530 Subject: [PATCH] solved --- LLCycle.java | 39 +++++++++++++++++++++++++++++++++++++++ RemoveNthNode.java | 28 ++++++++++++++++++++++++++++ ReverseLL.java | 29 +++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+) create mode 100644 LLCycle.java create mode 100644 RemoveNthNode.java create mode 100644 ReverseLL.java diff --git a/LLCycle.java b/LLCycle.java new file mode 100644 index 00000000..deaccdcc --- /dev/null +++ b/LLCycle.java @@ -0,0 +1,39 @@ +// Approach : Brute force approach is to solve using hashset, such that repetition of node +// gives the head of the cycle if exists +// Optimal Approach : solve using two pointer approach slow and fast, here slow = 1x, fast = 2x +// we can have 3x, 4x and so on speed for fast pointer as well +// here we have used the concept of rabbit and tortoise +// Time : O(2n) --> O(n), 1-n to check meet point, other n for checking equidistance +// Space : O(1) --> no extra space needed +// Code Successfully ran on leetcode + +class LLCycle { + public ListNode detectCycle(ListNode head) { + ListNode slow = head; + ListNode fast = head; + + boolean flag = false; + + while(fast != null && fast.next != null){ + slow = slow.next; + fast = fast.next.next; + + if(slow == fast){ // checking the meet-point + flag = true; + break; + } + } + + if(!flag) return null; + + slow = head; // relocating slow pointer for equi-distance check + + while(slow != fast){ + slow = slow.next; + fast = fast.next; + } + + return slow; + + } +} \ No newline at end of file diff --git a/RemoveNthNode.java b/RemoveNthNode.java new file mode 100644 index 00000000..32a5344d --- /dev/null +++ b/RemoveNthNode.java @@ -0,0 +1,28 @@ +// Brute Force Approach : Recursive Stack +// First Optimal Approach : solving using two pass approach such that no extra space is required +// Current Approach : solved using two pointer approach slow and fast such that +// keep moving the slow, fast pointer until we find the difference between listnode or count == n +// to handle edge case we use a dummy node -1. +// Time: O(n), Space: O(1) + +class Solution { + public ListNode removeNthFromEnd(ListNode head, int n) { + ListNode dummy = new ListNode(0); + dummy.next = head; + + ListNode slow = dummy; + ListNode fast = dummy; + + for(int i = 0; i <= n; i++){ + fast = fast.next; + } + + while(fast != null){ + slow = slow.next; + fast = fast.next; + } + + slow.next = slow.next.next; + return dummy.next; + } +} diff --git a/ReverseLL.java b/ReverseLL.java new file mode 100644 index 00000000..f72ad281 --- /dev/null +++ b/ReverseLL.java @@ -0,0 +1,29 @@ +// Approach 01: Brute force - traverse the entire linked list in forward manner +// store the ListNode one by one in a List, then traverse the List in reverse order +// and form the reverse linked list +// Time and Space : O(2n), Extra Space : O(n) + +// Approach 02 : Optimal One - using two variable apporach, reversing the linked list +// in the forward traversal itself keeping a prev, curr, temp variable(to keep track of next node) +// Time : O(n), Space: O(1) - no extra space needed + +// Yes this code successfully ran over leetcode + +// we can also use recursive stack approach but not optimal one. + + +class Solution { + public ListNode reverseList(ListNode head) { + ListNode prev = null; + ListNode curr = head; + + + while(curr != null){ + ListNode temp = curr.next; // keep track of next + curr.next = prev; // reverse + prev = curr; + curr = temp; + } + return prev; + } +}