Flatten a 2D linkedList . A node contains 3 parameters namely, data , pointer to left, pointer to down . The aim of the function is to flatten the linkedlist. Ex: 1-->2-->4 | V 3 Answer is 1-->2-->3-->4 Ex: 1 --> 2--> 3--> 4 | 5-->6-->7 | | 8 9 Answer is 1-->2->5 -> 8 -> 6->9 ->7-> 3-> 4 He asked me to make the same linkedlist flatten not to create a new LinkedList or print the elements
Anonymous
public static Dnode flatten(Dnode root){ if(root==null) return null; Dnode nextpoint= root.right; if(root.down!=null){ root.right=flatten(root.down); root.down=null; Dnode pointer=root.right; while(pointer.right!=null){ pointer=pointer.right; } pointer.right=nextpoint; } flatten(nextpoint); return root; }
Check out your Company Bowl for anonymous work chats.