[LeetCode]Merge Two Sorted Lists

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {  
public:  
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        ListNode *head = new ListNode(0);
        ListNode *h = head;
        ListNode *p, *q;
        p = l1;
        q = l2;

        while(1) {
            if(p == NULL) {
                h->next = q;
                break;
            } else if(q == NULL) {
                h->next = p;
                break;
            } else {
                if(p->val < q->val) {
                h->next = p;
                h = p;
                p = p->next;
            } else {
                h->next = q;
                h = q;
                q = q->next;
            }
            }
        }

        h = head->next;
        delete head;
        return h;
    }
};
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {  
public:  
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        ListNode *h, *head;
        ListNode *p, *q;
        p = l1;
        q = l2;

        if(p != NULL) {
            if(q != NULL) {
                if(p->val < q->val) {
                    h = p;
                    p = p->next;
                } else {
                    h = q;
                    q = q->next;
                }
            } else {
                h = p;
                p = p->next;
            }
        } else {
            if(q != NULL) {
                h = q;
                q = q->next;
            } else {
                return NULL;
            }
        }

        head = h;

        while(1) {
            if(p == NULL) {
                h->next = q;
                break;
            } else if(q == NULL) {
                h->next = p;
                break;
            } else {
                if(p->val < q->val) {
                h->next = p;
                h = p;
                p = p->next;
            } else {
                h->next = q;
                h = q;
                q = q->next;
            }
            }
        }

        return head;
    }
};
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {  
public:  
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        ListNode *head = new ListNode(0);
        ListNode *tmp;
        ListNode *p, *q;
        p = head;
        q = l2;
        head->next = l1;

        if(l1 == NULL) {
            return l2;
        } else if(l2 == NULL) {
            return l1;
        } else {
            while(q != NULL) {
                if(p->next != NULL) {
                    if(q->val < p->next->val) {
                        tmp = q->next;
                        q->next = p->next;
                        p->next = q;
                        q = tmp;
                    } else {
                        p = p->next;
                    }
                } else {
                    p->next = q;
                    break;
                }

            }
        }

        tmp = head->next;
        delete head;

        return tmp;
    }
};