diff --git a/java-program/FlattenLL.java b/java-program/FlattenLL.java new file mode 100644 index 0000000..c46ad1d --- /dev/null +++ b/java-program/FlattenLL.java @@ -0,0 +1,50 @@ +/** + * Author: AKSHAY WAKHARE + * Date: 22 October 2021 + */ + + +// QUESTION +// You are given a doubly linked list which in addition to the next and previous pointers, it could have a child pointer, which may or may not point to a separate doubly linked list. These child lists may have one or more children of their own, and so on, to produce a multilevel data structure, as shown in the example below. +// Flatten the list so that all the nodes appear in a single-level, doubly linked list. You are given the head of the first level of the list. + + + + +/* +class Node { + public int val; + public Node prev; + public Node next; + public Node child; +}; +*/ + + +//FUNCTION IMPLEMENTATION + public Node flatten(Node head) { + if( head == null) return head; + + Node p = head; + while( p!= null) { + /* CASE 1: if no child, proceed */ + if( p.child == null ) { + p = p.next; + continue; + } + + /* CASE 2: got child, find the tail of the child and link it to p.next */ + Node temp = p.child; + // Find the tail of the child + while( temp.next != null ) + temp = temp.next; + // Connect tail with p.next, if it is not null + temp.next = p.next; + if( p.next != null ) p.next.prev = temp; + // Connect p with p.child, and remove p.child + p.next = p.child; + p.child.prev = p; + p.child = null; + } + return head; + } \ No newline at end of file diff --git a/java-program/JumpGame.java b/java-program/JumpGame.java new file mode 100644 index 0000000..b1d4cc4 --- /dev/null +++ b/java-program/JumpGame.java @@ -0,0 +1,52 @@ +/** + * Author: AKSHAY WAKHARE + * Date: 22 October 2021 + */ + + +import java.util.*; +public class JumpGame{ + + public static void main(String args[]){ + JumpGame s = new JumpGame(); + int arr[] = {2,4,6,3,4,6}; + + if(s.isPosssible(arr)){ + System.out.println(s.minJumps(arr));} + else{ + System.out.println("Not Possible"); + } + } + + + public boolean isPosssible(int[] nums) { + + int n = nums.length; + + //last index from where we can reach to end cell + //As from last index (n - 1) itself reach to end cell, + int lastIndex = n - 1; + + for(int i = n - 1; i >= 0; i--){ + if(i + nums[i] >= lastIndex){ + lastIndex = i; + } + } + + //check from 0 reach to end cell + return lastIndex == 0; + } + + + +public int minJumps(int[] A) { + int jumps = 0, curEnd = 0, curFarthest = 0; + for (int i = 0; i < A.length - 1; i++) { + curFarthest = Math.max(curFarthest, i + A[i]); + if (i == curEnd) { + jumps++; + curEnd = curFarthest; + } + } + return jumps; +} \ No newline at end of file diff --git a/java-program/ReverseNodesinKGroup.java b/java-program/ReverseNodesinKGroup.java new file mode 100644 index 0000000..e372b36 --- /dev/null +++ b/java-program/ReverseNodesinKGroup.java @@ -0,0 +1,51 @@ +/** + * Author: AKSHAY WAKHARE + * Date: 22 October 2021 + */ + + + +// Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. +// k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes, in the end, should remain as it is. +// You may not alter the values in the list's nodes, only nodes themselves may be changed. + + +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; } + }*/ + +//FUNCTION IMPLEMENTATION + public ListNode reverseKGroup(ListNode head, int k) { + int c=k; + ListNode h=head; + ListNode s=head; + ListNode p=null; + while(s!=null){ + ListNode next=s.next; + s.next=p; + p=s; + k--; + if(k==0){ + h.next=reverseKGroup(next,c); + return s; + } + s=next; + } + + if(s==null&&k!=0){ + s=p; + p=null; + while(s!=null){ + ListNode next=s.next; + s.next=p; + p=s; + s=next; + + }return p; + }return null; + } +} \ No newline at end of file