leetcode Merge Two Sorted Lists

来源:互联网 发布:python 延时 毫秒 编辑:程序博客网 时间:2024/05/05 17:17
class Solution {public:    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {        if (l1==NULL) return l2;        if (l2==NULL) return l1;        ListNode* temp;        // Assume l1 < l2        if (l1->val > l2->val){            temp = l2;            l2   = l1;            l1   = temp;        }        ListNode* head = l1;        while(l2!=NULL){            while(l1->next!=NULL && ((l1->next)->val<= (l2->val))) l1 = l1->next;            temp = l1->next;            l1->next = l2;            l1 = l2;            l2 = temp;        }        return head;    }};

0 0
原创粉丝点击