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
45 changes: 45 additions & 0 deletions Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Time Complexity : O(n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : yes
// Any problem you faced while coding this : didnt clicked about usage of boolean flag.
// Your code here along with comments explaining your approach: using boolean flag to check if cycle exists, if yes then move slow pointer to head and move both slow and fast pointers one step at a time until they meet, the meeting point is the start of the cycle.
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode detectCycle(ListNode head) {
if(head == null) return null;
ListNode slow = head;
ListNode fast = head;

boolean flag = false;

while(fast.next != null && fast.next.next != null){
slow = slow.next;
fast = fast.next.next;

if(slow == fast){
flag = true;
break;
}
}

if(!flag) return null;

slow = head;
while(slow != fast){
slow = slow.next;
fast = fast.next;
}

return slow;
}
}
43 changes: 43 additions & 0 deletions removeNthnodefromLL.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Time Complexity : O(n)
// Space Complexity : O(1)
// 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: start with dummy node, and use two pointers, fast and slow. Move fast pointer n+1 steps ahead, then move both pointers until fast reaches the end. Slow will be at the node before the one to be removed. Adjust the next pointer of slow to skip the nth node from the end.
/**
* Definition for singly-linked list.
* public 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 removeNthFromEnd(ListNode head, int n) {
ListNode dummy = new ListNode(-1);
dummy.next = head;

ListNode slow = dummy;
ListNode fast = dummy;

int count = 0;

while(count <= n){
fast = fast.next;
count++;
}

while(fast != null){
slow = slow.next;
fast = fast.next;
}

ListNode temp = slow.next;
slow.next = slow.next.next;
temp.next = null;

return dummy.next;
}
}
39 changes: 39 additions & 0 deletions reverseLL.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Time Complexity : O(n)
// Space Complexity : O(n)
// Did this code successfully run on Leetcode : yes
// Any problem you faced while coding this : old linked list node was not set to null after popping from stack, which caused a cycle in the linked list

// Your code here along with comments explaining your approach: push all the nodes of the linked list into a stack, then pop them one by one and create a new linked list in reverse order. Set the next pointer of the last node to null to avoid cycles.
/**
* Definition for singly-linked list.
* public 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 curr = head;
Stack<ListNode> st = new Stack<>();

while(curr != null){
st.push(curr);
curr = curr.next;
}
ListNode reverseHead = new ListNode(-1);
curr = reverseHead;
while(!st.isEmpty()){
curr.next = st.pop();
curr = curr.next;
curr.next = null;
}

return reverseHead.next;
}
}
{

}