For problem Merge two sorted lists, below is the code: I can’t find an issue but code drills even the test run is giving runtime error?
import java.io.*;
import java.util.*;
// Custom list node. DO NOT MODIFY.
class ListNode<T> {
public T value;
public ListNode<T> next;
ListNode(T value) {
this.value = value;
}
}
class MergeSortedLists {
public ListNode<Integer> getMergedList(ListNode<Integer> list1, ListNode<Integer> list2) {
if(null == list1)
return list2;
if(null == list2)
return list1;
ListNode<Integer> ans = new ListNode<Integer>(Integer.valueOf(0));
ans.next = null;
while(null != list1 && null != list2){
if(list1.value < list2.value){
ans.next = list1;
list1 = list1.next;
}
else{
ans.next = list2;
list2 = list2.next;
}
}
if(list1 != null)
ans.next= list1;
if(list2 != null)
ans.next = list2;
return ans.next;
}
}