Skip to content
Open

solved #1774

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
39 changes: 39 additions & 0 deletions LLCycle.java
Original file line number Diff line number Diff line change
@@ -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;

}
}
28 changes: 28 additions & 0 deletions RemoveNthNode.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
29 changes: 29 additions & 0 deletions ReverseLL.java
Original file line number Diff line number Diff line change
@@ -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;
}
}