Merge two sorted linked lists

LeetCode problem 21. Merge Two Sorted Lists.

## Problem description

Merge two sorted linked lists and return it as a **sorted** list. The list should be made by splicing together the nodes of the first two lists.

Example 1
* **Input:** l1 = [1,2,4], l2 = [1,3,4]
* **Output:** [1,1,2,3,4,4]

Example 2
* **Input:** l1 = [], l2 = []
* **Output:** []

Example 3
* **Input:** l1 = [], l2 = [0]
* **Output:** [0]

Constraints
* The number of nodes in both lists is in the range `[0, 50]`.
* `-100 <= Node.val <= 100`
* Both `l1` and `l2` are sorted in **non-decreasing** order.

## Analysis

We can use two pointers `p1` and `p2` to traverse through each of the lists, starting the head of each.

For each iteration:

* If `p1` is null (has reached the end), then we pick `p2`, and make `p2` progress by one step.
* If `p2` is null (has reached the end), then we pick `p1`, and make `p1` progress by one step.
* If both of them are not null, then we pick the pointer with the smaller value, and make that pointer progress by one step.

Stop until both pointer have become null (reached the end).

For merging, we can create a dummy head, and let the merge tail pointer progress from it. Each time the tail points to a node that we have *already visited*, so we can relocate the link safely without disrupting the pointers `p1` and `p2`. There is no need to create new nodes – we are playing with the nodes *in-place*.

### Complexity analysis

#### Time complexity

We go over each node in the two linked lists exactly once. So the time complexity is **O(m + n)**, where *m* and *n* are the length of the two linked lists.

#### Space complexity

Stack
We only have used the dummy head, merge tail, `p1` and `p2`. Hence the space complexity on stack is O(1).
Heap
Only the dummy head needs to be allocated during the iteration. So the stack complexity on heap is O(1).
Total space complexity
**O(1)**.
## Solution

### C++

```
/\*\*
 \* Definition for singly-linked list.
 \* struct ListNode {
 \* int val;
 \* ListNode \*next;
 \* ListNode() : val(0), next(nullptr) {}
 \* ListNode(int x) : val(x), next(nullptr) {}
 \* ListNode(int x, ListNode \*next) : val(x), next(next) {}
 \* };
 \*/
class Solution {
public:
 ListNode\* mergeTwoLists(ListNode\* l1, ListNode\* l2) {
 if (l1 == nullptr) {
 return l2;
 }
 else if (l2 == nullptr) {
 return l1;
 }

 ListNode \*p1 = l1, \*p2 = l2;
 ListNode dummyHead(0), \*pMerge = &dummyHead;

 while ((p1 != nullptr) || (p2 != nullptr)) {
 ListNode \*pMergeNext = nullptr;
 // use p2 if list 1 has been consumed entirely
 if (p1 == nullptr) {
 pMergeNext = p2;
 p2 = p2->next;
 pMerge->next = pMergeNext;
 pMerge = pMergeNext;
 }
 // use p1 if list 2 has been consumed entirely
 else if (p2 == nullptr) {
 pMergeNext = p1;
 p1 = p1->next;
 pMerge->next = pMergeNext;
 pMerge = pMergeNext;
 }
 else {
 // pick the smaller one
 if (p1->val < p2->val) {
 pMergeNext = p1;
 p1 = p1->next;
 pMerge->next = pMergeNext;
 pMerge = pMergeNext;
 }
 else {
 pMergeNext = p2;
 p2 = p2->next;
 pMerge->next = pMergeNext;
 pMerge = pMergeNext;
 }
 }
 }

 return dummyHead.next;
 }
};

```

### Java

```
/\*\*
 \* Definition for singly-linked list.
 \* public class ListNode {
 \* int val;
 \* ListNode next;
 \* ListNode() {}
 \* ListNode(int val) { this.val = val; }
 \* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 \* }
 \*/
class Solution {
 public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
 if (l1 == null) {
 return l2;
 }
 else if (l2 == null) {
 return l1;
 }

 ListNode p1 = l1, p2 = l2;
 ListNode dummyHead = new ListNode(0);
 ListNode pMerge = dummyHead;

 while ((p1 != null) || (p2 != null)) {
 ListNode pMergeNext = null;
 // use p2 if list 1 has been consumed entirely
 if (p1 == null) {
 pMergeNext = p2;
 p2 = p2.next;
 pMerge.next = pMergeNext;
 pMerge = pMergeNext;
 }
 // use p1 if list 2 has been consumed entirely
 else if (p2 == null) {
 pMergeNext = p1;
 p1 = p1.next;
 pMerge.next = pMergeNext;
 pMerge = pMergeNext;
 }
 else {
 // pick the smaller one
 if (p1.val < p2.val) {
 pMergeNext = p1;
 p1 = p1.next;
 pMerge.next = pMergeNext;
 pMerge = pMergeNext;
 }
 else {
 pMergeNext = p2;
 p2 = p2.next;
 pMerge.next = pMergeNext;
 pMerge = pMergeNext;
 }
 }
 }

 return dummyHead.next;
 }
}

```