Merge two sorted linked lists and return it as a new sorted list. The new list should be made by splicing together the nodes of the first two lists.
Solutions:
(1) Recursive:
The recursive function mergeTwoLists(ListNode l1, ListNode l2) mean merged l1 and l2. So, with the head being chosen, merge the left list and another list recursively will solve the problem.
Java:
public ListNode mergeTwoLists(ListNode l1, ListNode l2){
if(l1 == null) return l2;
if(l2 == null) return l1;
if(l1.val < l2.val){
l1.next = mergeTwoLists(l1.next, l2);
return l1;
} else{
l2.next = mergeTwoLists(l1, l2.next);
return l2;
}
}
// https://leetcode.com/problems/merge-two-sorted-lists/discuss/9715/Java-1-ms-4-lines-codes-using-recursion
Python:
def mergeTwoLists2(self, l1, l2):
if not l1 or not l2:
return l1 or l2
if l1.val < l2.val:
l1.next = self.mergeTwoLists(l1.next, l2)
return l1
else:
l2.next = self.mergeTwoLists(l1, l2.next)
return l2
# https://leetcode.com/problems/merge-two-sorted-lists/discuss/9735/Python-solutions-(iteratively-recursively-iteratively-in-place).
C++:
class Solution {
public:
ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
if(l1 == NULL) return l2;
if(l2 == NULL) return l1;
if(l1->val < l2->val) {
l1->next = mergeTwoLists(l1->next, l2);
return l1;
} else {
l2->next = mergeTwoLists(l2->next, l1);
return l2;
}
}
};
// This solution is not a tail-recursive, the stack will overflow while the list is too long :) http://en.wikipedia.org/wiki/Tail_call
// https://leetcode.com/problems/merge-two-sorted-lists/discuss/9713/A-recursive-solution
(2) Iterative:
(2.1) use a dummy node and link the smallest-val node after the previous linked node
Java:
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if(l1 == null) return l2;
else if(l2 == null) return l1;
ListNode dummy = new ListNode(0);
ListNode curr = dummy;
while(l1 != null && l2!= null){
if(l1.val <= l2.val){
curr.next = l1;
l1 = l1.next;
}else {
curr.next = l2;
l2 = l2.next;
}
curr = curr.next;
}
curr.next = l1 == null? l2:l1;
return dummy.next;
}
// by davidluoyes https://leetcode.com/problems/merge-two-sorted-lists/discuss/9715/Java-1-ms-4-lines-codes-using-recursion
Python:
def mergeTwoLists1(self, l1, l2):
dummy = cur = ListNode(0)
while l1 and l2:
if l1.val < l2.val:
cur.next = l1
l1 = l1.next
else:
cur.next = l2
l2 = l2.next
cur = cur.next
cur.next = l1 or l2
return dummy.next
# https://leetcode.com/problems/merge-two-sorted-lists/discuss/9735/Python-solutions-(iteratively-recursively-iteratively-in-place).
(2.2) Merge l2 into l1
use p1 to save the previous node in l1
Java:
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if(l1 == null) return l2;
if(l2 == null) return l1;
ListNode p1 = null;
ListNode head = l1;
//Absorb l2 inside of l1
while(l1 != null && l2 != null) {
if(l1.val <= l2.val) {
p1 = l1;
l1 = l1.next;
} else {
//l2 < l1. So insert p1 -> l2 -> l1; make new p1 as l2, retain l1. and make l2 as old l2.next
if(p1 != null) {
p1.next = l2;
} else {
head = l2;
}
ListNode saveL2Next = l2.next;
l2.next = l1;
p1 = l2;
l2 = saveL2Next;
}
}
if(l1 == null) p1.next = l2;
//if l2 is null, do nothing, we have absorbed it inside l1
return head;
}
// by ftp1234 https://leetcode.com/problems/merge-two-sorted-lists/discuss/9715/Java-1-ms-4-lines-codes-using-recursion
Python:
def mergeTwoLists(self, l1, l2):
if None in (l1, l2):
return l1 or l2
dummy = cur = ListNode(0)
dummy.next = l1
while l1 and l2:
if l1.val < l2.val:
l1 = l1.next
else:
nxt = cur.next
cur.next = l2
tmp = l2.next
l2.next = nxt
l2 = tmp
cur = cur.next
cur.next = l1 or l2
return dummy.next
# https://leetcode.com/problems/merge-two-sorted-lists/discuss/9735/Python-solutions-(iteratively-recursively-iteratively-in-place).