LintCode-剑指Offer-(165)合并两个排序链表

来源:互联网 发布:mac输入法记忆 编辑:程序博客网 时间:2024/06/06 13:58
class Solution {public:    /**    * @param ListNode l1 is the head of the linked list    * @param ListNode l2 is the head of the linked list    * @return: ListNode head of linked list    */    ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {        // write your code here        ListNode* l3 = new ListNode(0);        ListNode* l4=l3;        ListNode* l5=l4;        while ( l1 != NULL&&l2 != NULL ){            if ( l1->val < l2->val ){                l3->next = l1;                l1 = l1->next;                l3 = l3->next;            }            else {                l3->next = l2;                l2 = l2->next;                l3 = l3->next;            }        }        if ( l1 == NULL ){            l3->next = l2;        }else            l3->next = l1;        delete l5;        return l4->next;    }};
0 0
原创粉丝点击