Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Umang Khambhalikar
/* Input: 1->2->4, 1->3->4 Output: 1->1->2->3->4->4 */ import java.util.*; import java.lang.*; import java.io.*; class ListNode{ public int value; public ListNode next; public ListNode(int value) { this.value = value; this.next = null; } } class ListHelper { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { ListNode outputNode = new ListNode(0); ListNode firstNode = new ListNode(0); int anchor=0; while(l1!=null || l2!=null) { //System.out.println(outputNode.value + " L1 value " + l1.value + " L2 value" + l2.value); ListNode temp = new ListNode(0); if (l1!=null && l2!=null){ if (l1.value<=l2.value) { temp.value=l1.value; outputNode.next=temp; l1=l1.next; outputNode=outputNode.next; } else { temp.value=l2.value; outputNode.next=temp; l2=l2.next; outputNode=outputNode.next; } } else if (l1==null && l2!=null) { while(l2!=null) { temp.value=l2.value; outputNode.next=temp; l2=l2.next; outputNode=outputNode.next; } } else if(l2==null && l1!=null) { while(l1!=null) { temp.value=l1.value; outputNode.next=temp; l1=l1.next; outputNode=outputNode.next; } } if (anchor==0) { outputNode=temp; firstNode=temp; } anchor++; } /* TODO: Enter code here Input: a: [1,2,4,10,33] b: [1,3,4,50] Output: res: [1,1,2,3, 4, 4,10,33, 50] 1. All input arrays are sorted 2. Arrays may have duplicate entries 3. Merged array should be sorted with duplicates */ return firstNode; } public int[] dedup(int[] nodeValues){ // dedup code here //[1,1,2,2,2,3,4,5] //[1,2,3,4,5] return null; } public ListNode toListNode(int[] nodeValues) { // Now convert that list into linked list ListNode dummyRoot = new ListNode(0); ListNode ptr = dummyRoot; for(int item : nodeValues) { ptr.next = new ListNode(item); ptr = ptr.next; } return dummyRoot.next; } public String toString(ListNode node) { if (node == null) { return "[]"; } String result = ""; while (node != null) { result += Integer.toString(node.value) + ", "; node = node.next; } return "[" + result.substring(0, result.length() - 2) + "]"; } } class Rextester { public static int[] stringToIntegerArray(String input) { input = input.trim(); input = input.substring(1, input.length() - 1); if (input.length() == 0) { return new int[0]; } String[] parts = input.split(","); int[] output = new int[parts.length]; for(int index = 0; index < parts.length; index++) { String part = parts[index].trim(); output[index] = Integer.parseInt(part); } return output; } public static void main(String args[]) throws Exception { ListHelper listHelper = new ListHelper(); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String line; while ((line = in.readLine()) != null) { int[] nodeValues = stringToIntegerArray(line); ListNode l1 = listHelper.toListNode(nodeValues); System.out.println("List 1:" + listHelper.toString(l1)); line = in.readLine(); int[] nodeValues2 = stringToIntegerArray(line); ListNode l2 = listHelper.toListNode(nodeValues2); System.out.println("List 2:" + listHelper.toString(l2)); ListNode ret = listHelper.mergeTwoLists(l1, l2); String out = listHelper.toString(ret); System.out.println("Merged:" + out); int[] deduped = listHelper.dedup(nodeValues); if(deduped!=null){ ListNode dedupedList = listHelper.toListNode(deduped); System.out.println("Deduped:" + listHelper.toString(dedupedList)); } } } }
run
|
edit
|
history
|
help
0
상속2
Application
piglatin
javaLP(Multi)
DFS in Graph
Guess Number
Fibonacci numbers 0 - 1_000
Java Class and Object
Jh
fb_series