Skip to content
Open
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
53 changes: 53 additions & 0 deletions Detectcycle.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
39 changes: 39 additions & 0 deletions RemoveFromEnd.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
39 changes: 39 additions & 0 deletions Reverselist.java
Original file line number Diff line number Diff line change
@@ -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;
}
}