本文共 857 字,大约阅读时间需要 2 分钟。
将两个升序链表合并为一个新的升序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
/** * 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 *res=new ListNode; ListNode *p=res; if(l2==NULL) return l1; if(l1==NULL) return l2; while(l1 && l2){ if(l1->val <= l2->val){ p->next=l1; l1=l1->next; } else{ p->next=l2; l2=l2->next; } p=p->next; } if(l1){ p->next=l1; } if(l2){ p->next=l2; } return res->next; }};
通过时间:
转载地址:http://uiemb.baihongyu.com/