# #21 [Leetcode.21](https://leetcode.com/problems/merge-two-sorted-lists/) Merge Two Sorted Lists
###### tags: `Linked list` `Easy`
## Problem
You are given the heads of two sorted linked lists `list1` and `list2`.
Merge the two lists in a one sorted list. The list should be made by splicing together the nodes of the first two lists.
Return the head of the merged linked list.
### Sample Input & Output
#### example 1

```javascript
Input: list1 = [1,2,4], list2 = [1,3,4]
Output: [1,1,2,3,4,4]
```
#### example 2
```javascript
Input: list1 = [], list2 = []
Output: []
```
#### example 3
```javascript
Input: list1 = [], list2 = [0]
Output: [0]
```
<br>
:::spoiler **Optimal Space & Time Complexity**
```
```
:::
<hr/>
## Solutions
### Most voted solution
```javascript=
var mergeTwoLists = function(l1, l2) {
let dummy = new ListNode();
let currTail = dummy;
while(l1 && l2) {
if(l1.val > l2.val) {
currTail.next = l2;
l2 = l2.next;
} else {
currTail.next = l1;
l1 = l1.next;
}
currTail = currTail.next;
}
currTail.next = l1 || l2;
return dummy.next;
};
```
<br>
---
### Everyone's
#### Totally misunderstood listed list but pass the rest...
:::spoiler 東
```javascript=
var mergeTwoLists = function(list1, list2) {
let head = null;
if (!list1 && list2) return list2;
if (!list2 && list1) return list1;
if (!list1 && !list2) return null;
if (list1.val < list2.val){
head = new ListNode(list1.val)
list1 = list1.next;
} else {
head = new ListNode(list2.val)
list2 = list2.next;
}
let currNode = head;
while(list1 !== null && list2 !== null){
if (list1.val < list2.val){
currNode.next = new ListNode(list1.val)
list1 = list1.next;
} else {
currNode.next = new ListNode(list2.val)
list2 = list2.next;
}
currNode = currNode.next;
}
if (list1 !== null) currNode.next = list1;
if (list2 !== null) currNode.next = list2;
return head;
};
```
:::
<br>
#### Fail the test and would like some discussion
Sorry... but please explain.
:::spoiler Hao
```javascript=
/**
* Why it fails the tests?
*/
var mergeTwoLists = function (list1, list2) {
const getHeadNode = () => {
if (!list1) return list2;
if (!list2) return list1;
return list1.value <= list2.value ? list1 : list2;
};
const headNode = getHeadNode();
if (headNode) {
let curListNode = headNode;
let otherListNode = headNode === list1 ? list2 : list1;
while (otherListNode) {
if (!curListNode.next || curListNode.next.value > otherListNode.value) {
const newCurListNextNode = otherListNode;
otherListNode = curListNode.next;
curListNode.next = newCurListNextNode;
};
curListNode = curListNode.next;
}
}
return headNode;
};
```
:::
<br>
:::spoiler Raiy
```javascript=
var mergeTwoLists = function(list1, list2) {
// time:O(m+n) space:O(m+n) n = list1 length, m = list2 length
if(!list1 || !list2){
return list1 ? list1:list2
}
let mergedList = new ListNode(0,null);
let currentNode = mergedList;
while(list1 && list2){
if(list1.val < list2.val){
currentNode.next = list1
list1 = list1.next
}else{
currentNode.next = list2
list2 = list2.next
}
currentNode = currentNode.next
}
if(!list1){
currentNode.next = list2
}else if(!list2){
currentNode.next = list1
}
return mergedList.next
};
```
:::
<br>
:::spoiler YC
- Usage of `??`
```javascript=
/**
* time: O(m+n) - where m is the number of nodes in the list1, n is the number of nodes in the list2
* space: O(m+n) - where m is the number of nodes in the list1, n is the number of nodes in the list2
*/
var mergeTwoLists = function(list1, list2) {
if(!list1) return list2;
if(!list2) return list1;
const dummy = [];
let curr = dummy;
while(list1 && list2){
if(list1.val < list2.val){
curr.next = list1;
list1 = list1.next;
}
else{
curr.next = list2;
list2 = list2.next;
}
curr = curr.next;
}
curr.next = list1 ?? list2;
return dummy.next;
};
```
:::
<br>
#### Very similar to the most voted solution
:::spoiler Becky
```javascript=
//time: O(n) | space: O(n)
var mergeTwoLists = function(list1, list2) {
const head = new ListNode();
currentNode = head;
while( list1 && list2){
if( list1.val >= list2.val){
currentNode.next = list2;
list2 = list2.next;
}else{
currentNode.next = list1;
list1 = list1.next;
}
currentNode = currentNode.next
}
//假設其中一個list先null
if (list1){
currentNode.next = list1
}else{
currentNode.next = list2
}
return head.next
};
```
:::
<br>
:::spoiler 月 (Very similar to best approach)
```javascript=
var mergeTwoLists = function(l1, l2) {
// Time O(n) Space O(n)
const result = new ListNode(0);
let pointer = result; // Why pointer can reaction to result?
while(l1 && l2){
if(l1.val < l2.val){
pointer.next = l1; // Why not l1.val?
l1 = l1.next;
} else {
pointer.next = l2;
l2 = l2.next
}
pointer = pointer.next;
}
pointer.next = l1 || l2;
return result.next
};
```
:::
<br>
---
## Supplement / Discussion
### Usage of dummy
- Initiate head and tail
- Make pointer from dummy
- Handle edge case of null input
```javascript
let dummy = new ListNode();
let currTail = dummy;
// some work
return dummy.next;
```
### Usage of `??`