From 2af28475de5418a4c6ceb3d1397fcf659e74e6f8 Mon Sep 17 00:00:00 2001 From: MeghaN28 Date: Sat, 2 May 2026 19:23:54 -0700 Subject: [PATCH] Linked List 1 --- Detectcycle.java | 53 ++++++++++++++++++++++++++++++++++++++++++++++ RemoveFromEnd.java | 39 ++++++++++++++++++++++++++++++++++ Reverselist.java | 39 ++++++++++++++++++++++++++++++++++ 3 files changed, 131 insertions(+) create mode 100644 Detectcycle.java create mode 100644 RemoveFromEnd.java create mode 100644 Reverselist.java diff --git a/Detectcycle.java b/Detectcycle.java new file mode 100644 index 00000000..cc35be57 --- /dev/null +++ b/Detectcycle.java @@ -0,0 +1,53 @@ +// Time Complexity :O(M+N) where M is the number of nodes before the cycle and +// N is the number of nodes in the cycle +// Space Complexity :O(1) as we are using constant space +// Did this code successfully run on Leetcode :Yes +// Any problem you faced while coding this :Why we moved the slow pointer to head after detecting the cycle and how it helps in finding the start of the cycle. + + +// Your code here along with comments explaining your approach +// We will use the Floyd's Tortoise and Hare algorithm to detect the cycle. +// We will have two pointers, slow and fast. +// We will move the slow pointer by one step and the fast pointer by two steps. +// If there is a cycle, the fast pointer will eventually meet the slow pointer. +// Once we detect the cycle, we will reset the slow pointer to the head of the list and move both pointers one step at a time until they meet again. +// The point at which they meet will be the start of the cycle. +// We will return the node where they meet as the start of the cycle. +// If there is no cycle, we will return null. +// The time complexity of this algorithm is O(M+N) where M is the number of nodes before the cycle and N is the number of nodes in the cycle. The space complexity is O(1) as we are using constant space. +class Solution { + public ListNode detectCycle(ListNode head) { + + if (head == null) return null; + + ListNode slow = head; + ListNode fast = head; + boolean hasCycle = false; + + // detect cycle + while (fast != null && fast.next != null) { + + slow = slow.next; + fast = fast.next.next; + + if (slow == fast) { + hasCycle = true; + break; + } + } + + // find cycle start + if (hasCycle) { + slow = head; + + while (slow != fast) { + slow = slow.next; + fast = fast.next; + } + + return slow; + } + + return null; + } +} \ No newline at end of file diff --git a/RemoveFromEnd.java b/RemoveFromEnd.java new file mode 100644 index 00000000..8f4724d6 --- /dev/null +++ b/RemoveFromEnd.java @@ -0,0 +1,39 @@ +// Time Complexity : O(N) where N is the number of nodes in the linked list +// Space Complexity :O(1) as we are using constant space +// Did this code successfully run on Leetcode :Yes +// Any problem you faced while coding this :No + + +// Your code here along with comments explaining your approach +// We will use two pointers, slow and fast. +// We will move the fast pointer n steps ahead of the slow pointer. +// Then we will move both pointers one step at a time until the fast pointer reaches the end of the list. +// At this point, the slow pointer will be at the node just before the node we want to delete. +// We will then delete the node by changing the next pointer of the slow pointer to skip the node we want to delete. +// Finally, we will return the head of the modified list. +class Solution { + public ListNode removeNthFromEnd(ListNode head, int n) { + + ListNode dummy = new ListNode(-1); + dummy.next = head; + + ListNode slow = dummy; + ListNode fast = dummy; + + // move fast n steps ahead + for (int i = 0; i < n; i++) { + fast = fast.next; + } + + // move both until fast reaches end + while (fast != null && fast.next != null) { + slow = slow.next; + fast = fast.next; + } + + // delete node + slow.next = slow.next.next; + + return dummy.next; + } +} \ No newline at end of file diff --git a/Reverselist.java b/Reverselist.java new file mode 100644 index 00000000..053c32e2 --- /dev/null +++ b/Reverselist.java @@ -0,0 +1,39 @@ +// Time Complexity :O(N) where N is the number of nodes in the linked list +// Space Complexity :O(1) as we are using constant space +// Did this code successfully run on Leetcode :yES +// Any problem you faced while coding this :NO + + +// Your code here along with comments explaining your approach +// Brute force is to use set. +// We will use three pointers, prev, curr and temp. +// We will initialize prev to null and curr to head. +// We will iterate through the linked list until curr is null. +// We will store the next node in temp and then reverse the link by pointing curr.next to prev. +// We will then move prev to curr and curr to temp. +// Finally, we will return prev as the new head of the reversed linked list. + +class ListNode { + int val; + ListNode next; + ListNode() {} + ListNode(int val) { this.val = val; } + ListNode(int val, ListNode next) { this.val = val; this.next = next; } +} + +class Solution { + public ListNode reverseList(ListNode head) { + + ListNode prev = null; + ListNode curr = head; + + while (curr != null) { + ListNode temp = curr.next; + curr.next = prev; + prev = curr; + curr = temp; + } + + return prev; + } +} \ No newline at end of file