diff --git a/DeleteNode.java b/DeleteNode.java new file mode 100644 index 00000000..42a49dd5 --- /dev/null +++ b/DeleteNode.java @@ -0,0 +1,21 @@ +//As we don't have access to prev node, we cannot delete curr node. Current node can be deleted only if previous node is present. +//Because curr node is a local varible --> stays in stack memeory +//link to next node points to reference present in heap memory +//If we modify curr to null, it changes only in local +//If we modify curr.next to null, it changes the one in heap memory +//As we don't know prev node, don't try to delete it. Just modify it +//TC: O(n); SC:O(1) +class Solution { + public void deleteNode(Node del) { + // code here + while(del.next!=null && del.next.next!=null){ + del.data=del.next.data; + del=del.next; + + } + + del.data=del.next.data; + del.next=null; + + } +} diff --git a/Leetcode_143.java b/Leetcode_143.java new file mode 100644 index 00000000..fd369393 --- /dev/null +++ b/Leetcode_143.java @@ -0,0 +1,111 @@ +//way1 +/** + * 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; } + * } + */ + + //Identify mid point + //Reverse second half + //Use two pointerss and rearrange it + //TC: O(n/2)+O(n/2)+O(n/2); SC:O(1) +class Solution { + public void reorderList(ListNode head) { + //identify mid + ListNode slow=head; + ListNode fast=head; + + + while(fast.next!=null && fast.next.next!=null){ + slow=slow.next; + fast=fast.next.next; + } + + //reverse second half + fast=slow.next; + slow.next=null; + + slow = reverse(fast); + + //Rearrange using 2 pointers + fast=head; + + while(slow!=null){ + ListNode temp1=fast.next; + fast.next=slow; + slow=slow.next; + fast.next.next=temp1; + fast=fast.next.next; + } + + } + + private ListNode reverse(ListNode curr){ + + ListNode prev=null; + + while(curr!=null){ + ListNode temp=curr.next; + curr.next=prev; + prev=curr; + curr=temp; + } + + return prev; + } +} + + +//way2 +/** + * 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; } + * } + */ + + //Identify mid point + //Store secind half in stack + //Use two pointerss and rearrange it + //TC: O(n); SC:O(n) +class Solution { + public void reorderList(ListNode head) { + //identify mid + ListNode slow=head; + ListNode fast=head; + + + while(fast.next!=null && fast.next.next!=null){ + slow=slow.next; + fast=fast.next.next; + } + + //reverse second half + fast=slow.next; + slow.next=null; + + Stack stack=new Stack(); + while(fast!=null){ + stack.add(fast); + fast=fast.next; + } + + fast=head; + while(!(stack.isEmpty())){ + ListNode temp1=fast.next; + fast.next=stack.pop(); + fast.next.next=temp1; + fast=fast.next.next; + } + + } +} diff --git a/Leetcode_160.java b/Leetcode_160.java new file mode 100644 index 00000000..ded559ef --- /dev/null +++ b/Leetcode_160.java @@ -0,0 +1,100 @@ +//way1 +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode(int x) { + * val = x; + * next = null; + * } + * } + */ + + //Put all nodes of one in Set + //Travel second ListNodes and check whether node is present in set or not + //If present, return it + //TC: O(m+n); SC: O(m) + +public class Solution { + public ListNode getIntersectionNode(ListNode headA, ListNode headB) { + Set set=new HashSet<>(); + + while(headA!=null){ + set.add(headA); + headA=headA.next; + } + + while(headB!=null){ + if(set.contains(headB)){ + return headB; + }else{ + headB=headB.next; + } + } + + return null; + + } +} + +//way2: +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode(int x) { + * val = x; + * next = null; + * } + * } + */ + + //Identify headA length, headB length + //Travel the one with highest length till both the lenths are equal + //Travel them together and return the one at meeting point + //TC: O(m+n); SC: O(1) + +public class Solution { + public ListNode getIntersectionNode(ListNode headA, ListNode headB) { + Set set=new HashSet<>(); + + ListNode curr=headA; + int countA=0; + while(curr!=null){ + countA+=1; + curr=curr.next; + } + + curr=headB; + int countB=0; + while(curr!=null){ + countB+=1; + curr=curr.next; + } + + ListNode currA=headA; + ListNode currB=headB; + + while(countA>countB){ + countA-=1; + currA=currA.next; + } + + while(countB>countA){ + countB-=1; + currB=currB.next; + } + + while(currA!=currB){ + currA=currA.next; + currB=currB.next; + } + + return currA; + + + + } +} diff --git a/Leetcode_173.java b/Leetcode_173.java new file mode 100644 index 00000000..66a30abf --- /dev/null +++ b/Leetcode_173.java @@ -0,0 +1,50 @@ +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode() {} + * TreeNode(int val) { this.val = val; } + * TreeNode(int val, TreeNode left, TreeNode right) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ +//Use a stack. Put all the left element in stack +//When next is called, get left elements of right node by calling dfs using curr node.right. Return curr.val +// Check whether stack is empty or not +//TC:O(1)-->amortized; SC:O(h) [h is height of tree] +class BSTIterator { + Stack stack; + public BSTIterator(TreeNode root) { + this.stack=new Stack<>(); + dfs(root); + } + + public int next() { + if(!(stack.isEmpty())){ + TreeNode curr=stack.pop(); + dfs(curr.right); + return curr.val; + + } + + return -1; + } + + private void dfs(TreeNode curr){ + if(curr==null){ + return; + } + stack.add(curr); + dfs(curr.left); + } + + public boolean hasNext() { + return !(stack.isEmpty()); + + } +}