diff --git a/Solution.java b/Solution.java new file mode 100644 index 00000000..4594a0e8 --- /dev/null +++ b/Solution.java @@ -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; + } +} diff --git a/removeNthnodefromLL.java b/removeNthnodefromLL.java new file mode 100644 index 00000000..06cdc88d --- /dev/null +++ b/removeNthnodefromLL.java @@ -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; + } +} diff --git a/reverseLL.java b/reverseLL.java new file mode 100644 index 00000000..3d91da03 --- /dev/null +++ b/reverseLL.java @@ -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 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; + } +} + { + +}