Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions Problem1
Original file line number Diff line number Diff line change
@@ -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<Integer> 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();
*/
57 changes: 57 additions & 0 deletions Problem2
Original file line number Diff line number Diff line change
@@ -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;

}
}
25 changes: 25 additions & 0 deletions Problem3
Original file line number Diff line number Diff line change
@@ -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;
}
}
65 changes: 65 additions & 0 deletions Problem4
Original file line number Diff line number Diff line change
@@ -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<skipSteps;i++){
headA=headA.next;
}}
else if(!isAbig && skipSteps != -1){
for(int i=0;i<skipSteps;i++){
headB=headB.next;
}
}

while(headA!=null){
if(headA==headB) return headA;
headA=headA.next;
headB=headB.next;
}

return headA;
}
}