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.
var mergeTwoLists = function(l1, l2) {
var p = l1;
var q = l2;
var k = null;
var head = null;
if(p == null && q == null)
return null;
if(p == null )
return q;
if(q == null )
return p;
while(p!=null && q!=null){
if(head == null) {
if(p.val <= q.val) {
head = p;
p = p.next;
} else {
head = q;
q = q.next;
}
k = head;
} else {
if(p.val <= q.val) {
k.next = p;
p = p.next;
} else {
k.next = q;
q = q.next;
}
k = k.next;
}
}
if(p!=null) {
k.next = p;
}
if(q!=null) {
k.next = q;
}
return head;
};