diff --git a/LinkedListcycle2.java b/LinkedListcycle2.java new file mode 100644 index 00000000..e3c056f4 --- /dev/null +++ b/LinkedListcycle2.java @@ -0,0 +1,49 @@ +// 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 +/* +Take slow and fast pointers to check if we have a cycle or not firstly. If we see slow and fast pointers +meeting each other, then we break the loop and update the flag to confirm cycle exists.Now, start the slow +pointer from head again and then move both one step at a time until they meet.Return the node of slow pointer. + */ +/** + * 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) { + 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) { + flag = true; + break; + } + } + + if(!flag) + return null; + + slow = head; + while(slow != fast) { + slow = slow.next; + fast = fast.next; + } + return slow; + } +} \ No newline at end of file diff --git a/RemoventhNodeFromList.java b/RemoventhNodeFromList.java new file mode 100644 index 00000000..308cdfb5 --- /dev/null +++ b/RemoventhNodeFromList.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 : no + +// Your code here along with comments explaining your approach +/* +Compute the overall size of the linked list and get the difference of the size and n to get the node +before the required node to be deleted. We iterate to reach that node position by decrementing the difference +variable.Now, we establish connection using next pointers and delete the required node. + */ +/** + * 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 curr = head; + int size = 0; + + while(curr != null) { + curr = curr.next; + size++; + } + + curr = head; + size = size - n; + + if(size == 0) + return head.next; + + while(size > 1) { + curr = curr.next; + size--; + } + curr.next = curr.next.next; + return head; + } +} \ No newline at end of file diff --git a/ReverseLinkedList.java b/ReverseLinkedList.java new file mode 100644 index 00000000..cf5d836f --- /dev/null +++ b/ReverseLinkedList.java @@ -0,0 +1,36 @@ +// 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 +/* +Maintain 2 pointers previous and current to iteratively connect in a way where current points to previous. +Make sure to store current's next element in a temporary node before this connection is made. Later, swap +the previous, current and temp nodes to continue the reversing process. + */ +/** + * 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) { + //Iteratively + 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