diff --git a/Problem1 b/Problem1 new file mode 100644 index 00000000..c6dafeeb --- /dev/null +++ b/Problem1 @@ -0,0 +1,51 @@ +/** + * 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; + * } + * } + */ + //TC On + //SC On +class BSTIterator { + TreeNode root; + Queue q = new LinkedList(); + + public BSTIterator(TreeNode root) { + this.root = root; + helper(root); + + } + + public int next() { + return q.poll(); + } + + public boolean hasNext() { + return !q.isEmpty(); + } + + private void helper(TreeNode root) { + if (root == null) + return ; + helper(root.left); + q.add(root.val); + helper(root.right); + + } +} + +/** + * Your BSTIterator object will be instantiated and called as such: + * BSTIterator obj = new BSTIterator(root); + * int param_1 = obj.next(); + * boolean param_2 = obj.hasNext(); + */ diff --git a/Problem2 b/Problem2 new file mode 100644 index 00000000..c6706290 --- /dev/null +++ b/Problem2 @@ -0,0 +1,57 @@ +/** + * 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; } + * } + */ + //TC On SC O1 +class Solution { + public void reorderList(ListNode head) { + ListNode slow = head; + ListNode fast = head; + //find mid + ListNode mid = mid(head); + // rev + ListNode revHead = revList(mid.next); + //map + mid.next=null; + fast=revHead; + while(fast!=null){ + ListNode temp=slow.next; + slow.next=fast; + fast=fast.next; + slow.next.next=temp; + slow=temp; + + } + + } + + private ListNode mid(ListNode head) { + ListNode slow = head; + ListNode fast = head; + while (fast != null && fast.next != null) { + slow = slow.next; + fast = fast.next.next; + } + return slow; + + } + + private ListNode revList(ListNode head) { + ListNode curr = head; + ListNode prev = null; + while (curr != null) { + ListNode temp = curr.next; + curr.next = prev; + prev = curr; + curr = temp; + } + return prev; + + } +} diff --git a/Problem3 b/Problem3 new file mode 100644 index 00000000..2dd76832 --- /dev/null +++ b/Problem3 @@ -0,0 +1,25 @@ +//tc On +//sc o1 + +/* +class Node +{ + int data ; + Node next; + Node(int d) + { + data = d; + next = null; + } +} +*/ + +class Solution { + public void deleteNode(Node del_node) { + // code here + Node temp=del_node.next; + del_node.data=temp.data; + del_node.next=temp.next; + temp=null; + } +} diff --git a/Problem4 b/Problem4 new file mode 100644 index 00000000..07334842 --- /dev/null +++ b/Problem4 @@ -0,0 +1,65 @@ +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode(int x) { + * val = x; + * next = null; + * } + * } + */ +// TC O(m+n) +SC O(1) +public class Solution { + public ListNode getIntersectionNode(ListNode headA, ListNode headB) { + + if (headA == headB) { + return headA; + } + + int countA = 0; + int countB = 0; + int skipSteps = -1; + boolean isAbig = false; + ListNode tempA=headA; + + ListNode tempB=headB; + while (headA.next != null) { + headA=headA.next; + countA++; + + } + while (headB.next != null) { + headB=headB.next; + countB++; + } + if (countA > countB) { + skipSteps = countA - countB; + isAbig = true; + } + if (countB > countA) { + skipSteps = countB - countA; + } + headA=tempA; + headB=tempB; + + if (isAbig && skipSteps != -1) { + for(int i=0;i