I solved this problem:
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
How can I improve its execution time and also, if there is any better way of doing it?
public ListNode mergeTwoLists(ListNode listNode1, ListNode listNode2) {
ListNode headNode = new ListNode(0);
ListNode node = headNode;
while (null != listNode1 && null != listNode2) {
if (listNode1.val < listNode2.val) {
node.next = listNode1;
listNode1 = listNode1.next;
} else {
node.next = listNode2;
listNode2 = listNode2.next;
}
node = node.next;
}
if(null != listNode1) {
node.next = listNode1;
}
if(null != listNode2) {
node.next = listNode2;
}
return headNode.next;
}