From 20337fcece62968877a12bb7254829fc295fb64c Mon Sep 17 00:00:00 2001 From: Sreeja-99 <75175169+Sreeja-99@users.noreply.github.com> Date: Wed, 21 Jan 2026 12:10:12 -0600 Subject: [PATCH 1/4] Add deleteNode method to delete a node in linked list Implement the deleteNode method to remove a node from a linked list without access to the previous node. --- DeleteNode.java | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 DeleteNode.java 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; + + } +} From c723122d27048c4cd9f7bf22e9fe924f6f3c42be Mon Sep 17 00:00:00 2001 From: Sreeja-99 <75175169+Sreeja-99@users.noreply.github.com> Date: Wed, 21 Jan 2026 12:11:24 -0600 Subject: [PATCH 2/4] Implement BSTIterator for in-order traversal --- Leetcode_173.java | 50 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 Leetcode_173.java 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()); + + } +} From 6a09f8ad8722aca8ff849bc67aba43f20a4c0513 Mon Sep 17 00:00:00 2001 From: Sreeja-99 <75175169+Sreeja-99@users.noreply.github.com> Date: Wed, 21 Jan 2026 12:13:10 -0600 Subject: [PATCH 3/4] Implement reorderList for singly-linked list --- Leetcode_143.java | 111 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 Leetcode_143.java 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; + } + + } +} From aa98e9148a62f1dbd5015917f7513fea12a79c89 Mon Sep 17 00:00:00 2001 From: Sreeja-99 <75175169+Sreeja-99@users.noreply.github.com> Date: Wed, 21 Jan 2026 12:14:26 -0600 Subject: [PATCH 4/4] Add intersection node finder for linked lists Implement two methods to find the intersection of two singly-linked lists. The first method uses a set to track nodes from the first list, while the second method compares lengths and synchronizes traversal. --- Leetcode_160.java | 100 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 Leetcode_160.java 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; + + + + } +}